Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
#43 Download haxe compiler automatically
- First try
  • Loading branch information
soywiz committed Jun 5, 2016
1 parent 13f24ec commit 8f84d78
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 181 deletions.
8 changes: 0 additions & 8 deletions .travis.yml
Expand Up @@ -11,22 +11,14 @@ before_install:

install:
- sudo apt-get -y install php5
- curl -s -S -O http://haxe.org/website-content/downloads/3.2.1/downloads/haxe-3.2.1-linux64.tar.gz
- tar xzf haxe-3.2.1-linux64.tar.gz
- mvn clean --settings settings.xml
- nvm install 4 || true
- nvm use 4 || true

before_script:
- export HAXE_STD_PATH=$PWD/haxe-3.2.1/std
- export HAXEPATH=$PWD/haxe-3.2.1
- export PATH=$HAXEPATH:$PATH
- ls -la $PWD
- ls -la $PWD/haxe-3.2.1
- which java || true
- java -version || true
- which haxe || true
- haxe -version || true
- which nvm || true
- nvm --version || true
- which node || true
Expand Down
Expand Up @@ -19,6 +19,7 @@ package com.jtransc.tools
import com.jtransc.ast.*
import com.jtransc.error.invalidOp
import com.jtransc.error.noImpl
import com.jtransc.gen.haxe.HaxeCompiler
import com.jtransc.gen.haxe.HaxeLib
import com.jtransc.io.createZipFile
import com.jtransc.text.Indenter
Expand Down Expand Up @@ -110,6 +111,7 @@ object HaxeTools {

println("haxe " + haxeargs)

HaxeCompiler.ensureHaxeCompilerVfs()
vfs.passthru("haxe", haxeargs)

//val file = HaxeTools::class.java.getResourceAsStream("sample_lime_neko.xml")
Expand Down
65 changes: 65 additions & 0 deletions jtransc-gen-haxe/src/com/jtransc/gen/haxe/HaxeCompiler.kt
@@ -0,0 +1,65 @@
package com.jtransc.gen.haxe

import com.jtransc.JTranscSystem
import com.jtransc.error.invalidOp
import com.jtransc.log.log
import com.jtransc.vfs.*
import java.io.File
import java.net.URL

object HaxeCompiler {
val HaxeVersion = "3.3.0-rc.1"

val haxeCompilerFile: String by lazy {
//"haxe-$HaxeVersion-linux32.tar.gz"
if (JTranscSystem.isWindows()) "haxe-$HaxeVersion-win.zip"
else if (JTranscSystem.isMac()) "haxe-$HaxeVersion-osx.tar.gz"
else if (JTranscSystem.isLinux() && JTranscSystem.isOs32()) "haxe-$HaxeVersion-linux32.tar.gz"
else if (JTranscSystem.isLinux() && JTranscSystem.isOs64()) "haxe-$HaxeVersion-linux64.tar.gz"
else invalidOp("Not supported operaning system. Just supporting windows, osx and linux.")
}

//http://haxe.org/website-content/downloads/3.3.0-rc.1/downloads/haxe-3.3.0-rc.1-win.zip
val haxeCompilerUrl: URL by lazy { URL("http://haxe.org/website-content/downloads/$HaxeVersion/downloads/$haxeCompilerFile") }
val jtranscFolder: String by lazy { JTranscSystem.getUserHome() + "/.jtransc" }

val haxeCompilerUrlVfs: SyncVfsFile by lazy { UrlVfs(haxeCompilerUrl) }
val haxeCompilerLocalFileVfs: SyncVfsFile by lazy { LocalVfsEnsureDirs(File(jtranscFolder)).access(haxeCompilerFile) }

val haxeCompilerLocalFolderVfs: SyncVfsFile by lazy { LocalVfsEnsureDirs(File("$jtranscFolder/$HaxeVersion")) }

fun getHaxeTargetLibraries(subtarget: String): List<String> = when (subtarget) {
"cpp", "windows", "linux", "mac", "osx" -> listOf("hxcpp")
"c#", "cs", "csharp" -> listOf("hxcs")
"java", "jvm" -> listOf("hxjava")
else -> listOf()
}


fun ensureHaxeCompilerVfs(): SyncVfsFile {
log.info("ensureHaxeCompilerVfs:")
if (!haxeCompilerLocalFileVfs.exists) {
log.info("Downloading haxe: $haxeCompilerUrl...")
haxeCompilerUrlVfs.copyTo(haxeCompilerLocalFileVfs)
}
if (!haxeCompilerLocalFolderVfs["std"].exists) {
val compvfs = CompressedVfs(haxeCompilerLocalFileVfs.realfile)
val compvfsBase = compvfs.firstRecursive { it.file.name == "std" }.file.parent
compvfsBase.copyTreeTo(haxeCompilerLocalFolderVfs)
}

if (!haxeCompilerLocalFolderVfs["lib"].exists) {
haxeCompilerLocalFolderVfs.passthru("haxelib", "setup", haxeCompilerLocalFolderVfs["lib"].realpathOS)
}

return haxeCompilerLocalFolderVfs
}

fun ensureHaxeSubtarget(subtarget: String) {
ensureHaxeCompilerVfs()

for (lib in getHaxeTargetLibraries(subtarget)) {
HaxeLib.installIfNotExists(HaxeLib.LibraryRef.fromVersion(lib))
}
}
}
17 changes: 15 additions & 2 deletions jtransc-gen-haxe/src/com/jtransc/gen/haxe/haxe.kt
Expand Up @@ -115,6 +115,7 @@ object HaxeGenTools {
}

val GenTargetInfo.mergedAssetsFolder: File get() = File("${this.targetDirectory}/merged-assets")
val cmpvfs: SyncVfsFile by lazy { HaxeCompiler.ensureHaxeCompilerVfs() }

class HaxeTemplateString(val names: HaxeNames, val tinfo: GenTargetInfo, val settings: AstBuildSettings, val actualSubtarget: HaxeAddSubtarget) {
val program = tinfo.program
Expand Down Expand Up @@ -152,7 +153,9 @@ class HaxeTemplateString(val names: HaxeNames, val tinfo: GenTargetInfo, val set
"extra" to settings.extra
)


init {
HaxeCompiler.ensureHaxeSubtarget(actualSubtarget.name)
params["defaultBuildCommand"] = {
Minitemplate("""
haxe
Expand Down Expand Up @@ -346,8 +349,18 @@ class HaxeGenTargetProcessor(val tinfo: GenTargetInfo, val settings: AstBuildSet
log("- ${cmd.joinToString(" ")}")
}
for (cmd in cmdList) {
log("Executing: ${cmd.joinToString(" ")}")
val processResult = ProcessUtils.runAndRedirect(buildVfs.realfile, cmd)
val commandRaw = cmd.first()
val cmdArgs = cmd.drop(1)

val command = when (commandRaw) {
"haxe" -> cmpvfs["haxe"].realpathOS
"haxelib" -> cmpvfs["haxelib"].realpathOS
else -> commandRaw
}

log("Executing: $command ${cmdArgs.joinToString(" ")}")

val processResult = ProcessUtils.runAndRedirect(buildVfs.realfile, command, cmdArgs)
if (!processResult.success) return ProcessResult2(processResult.exitValue)
}
return if (run && !buildAndRunAsASingleCommand) this.run(redirect) else ProcessResult2(0)
Expand Down
24 changes: 16 additions & 8 deletions jtransc-gen-haxe/src/com/jtransc/gen/haxe/haxelib.kt
@@ -1,33 +1,41 @@
package com.jtransc.gen.haxe

import com.jtransc.text.toUcFirst
import com.jtransc.vfs.CwdVfs

object HaxeLib {
data class LibraryRef(val name: String, val version: String) {
val id = name.toUcFirst() // Remove symbols!
val nameWithVersion = "$name:$version"
val nameWithVersion = if (version.isNotEmpty()) "$name:$version" else "$name"

companion object {
fun fromVersion(it:String): LibraryRef {
fun fromVersion(it: String): LibraryRef {
val parts = it.split(':')
return LibraryRef(parts[0], parts[1])
return LibraryRef(parts.getOrElse(0) { "" }, parts.getOrElse(1) { "" })
}
}
}

val vfs by lazy { CwdVfs() }
val vfs by lazy { HaxeCompiler.ensureHaxeCompilerVfs() }

fun exists(lib: LibraryRef):Boolean {
fun exists(lib: LibraryRef): Boolean {
return vfs.exec("haxelib", "--always", "path", lib.nameWithVersion).success
}

fun install(lib: LibraryRef) {
vfs.passthru("haxelib", "--always", "install", lib.name, lib.version)
if (lib.version.isEmpty()) {
vfs.passthru("haxelib", "--always", "install", lib.name)
} else {
vfs.passthru("haxelib", "--always", "install", lib.name, lib.version)
}
}

//fun downloadHaxeCompiler() {
//}

fun installIfNotExists(lib: LibraryRef) {
if (!exists(lib)) {
install(lib)
}
}
}
}

10 changes: 10 additions & 0 deletions jtransc-gen-haxe/test/com/jtransc/gen/haxe/CompilerTest.kt
@@ -0,0 +1,10 @@
package com.jtransc.gen.haxe

import org.junit.Test

class CompilerTest {
@Test
fun testEnsureHaxeCompilerVfs() {
HaxeCompiler.ensureHaxeCompilerVfs()
}
}
17 changes: 17 additions & 0 deletions jtransc-rt-core/src/com/jtransc/JTranscSystem.java
Expand Up @@ -171,12 +171,25 @@ static public String getOS() {
return os;
}

// http://lopica.sourceforge.net/os.html
@HaxeMethodBody("return HaxeNatives.str('x86');")
static public String getArch() {
// x86, i386, ppc, sparc, arm
return System.getProperty("os.arch");
}

public static boolean isOs32() {
return !isOs64();
}

public static boolean isOs64() {
if (System.getProperty("os.name").contains("Windows")) {
return (System.getenv("ProgramFiles(x86)") != null);
} else {
return (System.getProperty("os.arch").indexOf("64") != -1);
}
}

public static boolean isWindows() {
return getOS().toLowerCase().startsWith("win");
}
Expand All @@ -203,4 +216,8 @@ public static String lineSeparator() {
//return isWindows() ? "\r\n" : "\n";
return "\n";
}

static public String getUserHome() {
return System.getProperty("user.home");
}
}
29 changes: 23 additions & 6 deletions jtransc-utils/src/com/jtransc/io/stream.kt
Expand Up @@ -2,39 +2,56 @@ package com.jtransc.io

import java.io.InputStream
import java.io.OutputStream
import java.nio.charset.Charset
import java.util.*

fun OutputStream.i8(value:Int) {
fun OutputStream.i8(value: Int) {
this.write(value.toInt())
}

fun OutputStream.i16(value:Int) {
fun OutputStream.i16(value: Int) {
this.write((value.toInt() ushr 8) and 0xFF)
this.write((value.toInt() ushr 0) and 0xFF)
}

fun OutputStream.i32(value:Int) {
fun OutputStream.i32(value: Int) {
this.write((value.toInt() ushr 24) and 0xFF)
this.write((value.toInt() ushr 16) and 0xFF)
this.write((value.toInt() ushr 8) and 0xFF)
this.write((value.toInt() ushr 0) and 0xFF)
}

fun InputStream.i8():Int {
fun InputStream.i8(): Int {
return (this.read() and 0xFF).toByte().toInt()
}

fun InputStream.i16():Int {
fun InputStream.i16(): Int {
val h = i8()
val l = i8()
return ((h shl 8) or (l shl 0)).toShort().toInt()
}

fun InputStream.i32(value:Int):Int {
fun InputStream.i32(value: Int): Int {
val h = i16()
val l = i16()
return ((h shl 16) or (l shl 0)).toInt().toInt()
}

fun InputStream.bytes(count: Int): ByteArray {
val out = ByteArray(count)
this.read(out)
return out
}

fun ByteArray.stripTailZeros():ByteArray {
val index = this.indexOf(0)
return Arrays.copyOf(this, if (index >= 0) index else this.size)
}

fun InputStream.stringz(count: Int, charset: Charset = Charsets.UTF_8): String {
return this.bytes(count).stripTailZeros().toString(charset)
}

val EmptyByteArray = ByteArray(0)
fun InputStream.readAvailableChunk(): ByteArray {
if (this.available() <= 0) return EmptyByteArray
Expand Down
18 changes: 18 additions & 0 deletions jtransc-utils/src/com/jtransc/numeric/multiples.kt
@@ -0,0 +1,18 @@
package com.jtransc.numeric

private fun roundUp(numToRound:Int, multiple:Int): Int
{
if (multiple == 0)
return numToRound;

val remainder = Math.abs(numToRound) % multiple;
if (remainder == 0)
return numToRound;

if (numToRound < 0)
return -(Math.abs(numToRound) - remainder);
else
return numToRound + multiple - remainder;
}

fun Int.nextMultipleOf(multiple:Int):Int = roundUp(this, multiple)
9 changes: 9 additions & 0 deletions jtransc-utils/src/com/jtransc/numeric/parse.kt
@@ -0,0 +1,9 @@
package com.jtransc.numeric

fun String.toInt(radix: Int) = Integer.parseInt(this, radix)

fun String.toInt(radix: Int, default: Int) = try {
Integer.parseInt(this, radix)
} catch (t: Throwable) {
default
}

0 comments on commit 8f84d78

Please sign in to comment.