Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hlc] Implement automatic compilation with make and Build.xml templates #706

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions other/haxelib/Run.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Build {

var output : String;
var name : String;
var sourcesDir : String;
var targetDir : String;
var dataPath : String;
var config : {
Expand All @@ -17,17 +18,34 @@ class Build {
this.dataPath = dataPath;
var path = new haxe.io.Path(output);
this.name = path.file;
this.targetDir = path.dir+"/";
this.sourcesDir = path.dir+"/";
this.targetDir = this.sourcesDir;
}

function log(message:String) {
if( config.defines.get("hlgen.silent") == null )
Sys.println(message);
}


public function run() {
var tpl = config.defines.get("hlgen.makefile");
if( tpl != null )
generateTemplates(tpl);
if( config.defines.get("hlgen.silent") == null )
Sys.println("Code generated in "+output+" automatic native compilation not yet implemented");
switch tpl {
case "make":
Sys.command("make", ["-C", targetDir]);
case "hxcpp":
Sys.command("haxelib", ["--cwd", targetDir, "run", "hxcpp", "Build.xml"].concat(config.defines.exists("debug") ? ["-Ddebug"] : []));
case null:
log('Code generated in $output');
log('Set hlgen.makefile for automatic native compilation');
case unimplemented:
log('Code generated in $output');
log('Automatic native compilation not yet implemented for $unimplemented');
}
}

function isAscii( bytes : haxe.io.Bytes ) {
// BOM CHECK
if( bytes.length > 3 && bytes.get(0) == 0xEF && bytes.get(1) == 0xBB && bytes.get(2) == 0xBF )
Expand All @@ -45,21 +63,19 @@ class Build {
if( tpl == null || tpl == "1" )
tpl = "vs2015";
var srcDir = tpl;
var targetDir = config.defines.get("hlgen.makefilepath");
var relDir = "";
if( targetDir == null )
targetDir = this.targetDir;
else {
if( config.defines["hlgen.makefilepath"] != null ) {
targetDir = config.defines.get("hlgen.makefilepath");
if( !StringTools.endsWith(targetDir,"/") && !StringTools.endsWith(targetDir,"\\") )
targetDir += "/";
var sourcesAbs = sys.FileSystem.absolutePath(sourcesDir);
var targetAbs = sys.FileSystem.absolutePath(targetDir);
var currentAbs = sys.FileSystem.absolutePath(this.targetDir);
if( !StringTools.startsWith(currentAbs, targetAbs+"/") )
relDir = currentAbs+"/"; // absolute
else
relDir = currentAbs.substr(targetAbs.length+1);
if( !StringTools.startsWith(sourcesAbs, targetAbs+"/") )
relDir = sourcesAbs+"/"; // absolute
else
relDir = sourcesAbs.substr(targetAbs.length+1);
relDir = relDir.split("\\").join("/");
if( relDir != "" )
if( !(relDir == "" || StringTools.endsWith(relDir, "/")) )
relDir += "/";
}
if( !sys.FileSystem.exists(srcDir) ) {
Expand All @@ -72,7 +88,7 @@ class Build {
for( f in config.files )
if( StringTools.endsWith(f,".c") ) {
var h = f.substr(0,-2) + ".h";
if( sys.FileSystem.exists(targetDir+h) )
if( sys.FileSystem.exists(sourcesDir+h) )
allFiles.push(h);
}
allFiles.sort(Reflect.compare);
Expand Down Expand Up @@ -137,7 +153,7 @@ class Build {
return sha1.substr(0,8)+"-"+sha1.substr(8,4)+"-"+sha1.substr(12,4)+"-"+sha1.substr(16,4)+"-"+sha1.substr(20,12);
},
makePath : function(_,dir:String) {
return dir == "" ? "./" : (StringTools.endsWith(dir,"/") || StringTools.endsWith(dir,"\\")) ? dir : dir + "/";
return dir == "" ? "." : (StringTools.endsWith(dir,"/") || StringTools.endsWith(dir,"\\")) ? dir : dir + "/";
},
upper : function(_,s:String) {
return s.charAt(0).toUpperCase() + s.substr(1);
Expand Down Expand Up @@ -179,4 +195,4 @@ class Run {
Sys.exit(1);
}
}
}
}
39 changes: 39 additions & 0 deletions other/haxelib/templates/hxcpp/Build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<xml>
<files id="::name::">
<compilerflag value="-std=c11" unless="MSVC_VER" />
<compilerflag value="-DHL_MAKE" />
<compilerflag value="-I$$makePath(::relDir::)" />
<compilerflag value="-I${HASHLINK}/src" if="HASHLINK" />
<compilerflag value="-I${HASHLINK}/include" if="HASHLINK" />
::foreach hfiles::
<depend name="::relDir::::path::" />::end::
::foreach cfiles::
<file name="::relDir::::path::" />::end::
</files>

<set name="ARCH_DIR" value="/x64" if="HXCPP_M64" />

<target id="default" output="::name::" tool="linker" toolid="exe">
<files id="::name::" />

<libpath name="${HASHLINK}" if="HASHLINK" />
<libpath name="${HASHLINK}/lib" if="HASHLINK" />

<section unless="windows">
<lib name="-lhl" />
<lib name="-lm" />
<lib name="-luv" />
::foreach libraries::
<lib name="-l:::name::.hdll" />::end::
</section>

<section if="windows">
<libpath name="${HASHLINK}${ARCH_DIR}/Debug" if="debug HASHLINK" />
<libpath name="${HASHLINK}${ARCH_DIR}/Release" if="HASHLINK" unless="debug" />

<lib name="libhl.lib" />
::foreach libraries::
<lib name="::name::.lib" />::end::
</section>
</target>
</xml>
37 changes: 37 additions & 0 deletions other/haxelib/templates/make/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Auto-generated from hashlink's Makefile template; do not edit

CFLAGS=-O3 -std=c11 -DHL_MAKE -I$$makePath(::relDir::)
LDLIBS=-lhl -lm -luv::foreach libraries:: -l:::name::.hdll::end::

ifdef HASHLINK
CFLAGS+=-I$(HASHLINK)/include -I$(HASHLINK)/src
LDFLAGS+=-L$(HASHLINK)/lib -L$(HASHLINK)
endif

ifeq ($(OS),Windows_NT)
LDFLAGS+=-municode
LDLIBS+=-ldbghelp
EXE_SUFFIX=.exe
endif

SRC_FILES=::foreach cfiles:: \
::path::::end::

OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES))

DEP_FILES=$(patsubst %.c,%.d,$(SRC_FILES))

::name::$(EXE_SUFFIX): $(OBJ_FILES)
$(CC) $(OBJ_FILES) -o $@ $(LDFLAGS) $(LDLIBS)

-include $(DEP_FILES)

%.o:
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -MMD -MP -c ::relDir::$*.c -o $@

clean:
find . \( -name '*.o' -o -name '*.d' \) -delete
rm ::name::$(EXE_SUFFIX)

.SUFFIXES: