Skip to content

Instantly share code, notes, and snippets.

@darmie
Last active September 22, 2023 16:57
Show Gist options
  • Save darmie/99fbd3512b49d0b8cc11fb93bf2b3f8c to your computer and use it in GitHub Desktop.
Save darmie/99fbd3512b49d0b8cc11fb93bf2b3f8c to your computer and use it in GitHub Desktop.
HXCPP Cross Compile to Windows from Mac OSX
<!--
This file will get included twice - once at the beginning - once at the end.
The first time though, the "vars" section is parsed, and this is where you can
set variables to control the setup of the standard compilers and install paths etc.
The second time, the "exes" section is parsed, and you can modify the linkers/compilers
by adding flags to these executables.
-->
<xml>
<!-- This section is parses at the beginning of the build .... -->
<section id="vars">
<!-- Use this to control the SDK used when compiling for iphone/iphonesim, and the
default is not found, or you wish to use a specific version -->
<!-- <set name="IPHONE_VER" value="4.2" /> -->
<!-- If you put all your SDKs in one directory, then you can just set this
single variable. If you put them in exactly this place, then you should not
need to setup anything ! -->
<set name="SDK_ROOT" value="c:\SDKs\" unless="SDK_ROOT" if="windows" />
<set name="SDK_ROOT" value="${HOME}/Library/Android" unless="SDK_ROOT" />
<!-- ANDROID_NDK_ROOT is required for building android binaries. See:
http://developer.android.com/sdk/ndk/index.html
Alternatively, you can set the directory and let hxcpp choose the best one with ANDROID_NDK_DIR
if ANDROID_NDK_DIR is not set, or a valid NDK version is not found there,
it tries to use the default SDK Manager installation (ndk-bundle directory)
<set name="ANDROID_NDK_ROOT" value="${SDK_ROOT}/android-ndk-r6" unless="ANDROID_NDK_ROOT" />
-->
<set name="ANDROID_NDK_ROOT" value="${SDK_ROOT}/ndk" unless="ANDROID_NDK_ROOT" />
<set name="ANDROID_NDK_DIR" value="${SDK_ROOT}/ndk" unless="ANDROID_NDK_DIR" />
<set name="NDK" value="17" />
<!-- ant is required to build android packages. See:
http://ant.apache.org/bindownload.cgi -->
<set name="ANT_HOME" value="${SDK_ROOT}/ant" unless="ANT_HOME" />
<!-- The ANDROID_SDK is required for building android packages. See
http://developer.android.com/sdk/index.html -->
<set name="ANDROID_SDK" value="${SDK_ROOT}/sdk" unless="ANDROID_SDK" />
<!-- The Java development kit (which should include the java runtime) is
also required for android dev. Make sure to get the 32 bit version! -->
<set name="JAVA_HOME" value="${SDK_ROOT}/java_jdk" unless="JAVA_HOME" />
<set name="toolchain" value="mingw" />
<set name="MINGW_ROOT" value="/usr/local/cellar/mingw-w64/6.0.0_2/bin" />
<!-- If you want to control how many compilers get spawned .
The default will use the number of processors.
Reducing this to 1 can help deciphering error messages. -->
<!-- <set name="HXCPP_COMPILE_THREADS" value="1" /> -->
<!-- Add this if your compiler does not support precompiled headers -->
<set name="NO_PRECOMPILED_HEADERS" value="1"/>
<!-- Older versions of gcc do not suport the @file syntax for an obj file list
and precompiled headers. Set this if you have these issues -->
<!-- <set name="GCC_OLD" value="1" /> -->
<!-- Skip the "stripping" phase. This will leave extra symbols in you exe, which
may allow reverse engineering and larger file sizes. You might want to do
this to debug some problems though. -->
<set name="nostrip" value="1" />
<!-- Let the system detect the latest version of MSVC on windows
(otherwise must setup the environment yourself) -->
<!-- Disable with <set name="NO_AUTO_MSVC" value="1" /> -->
<!-- To disable specific versions: <setenv name="VS110COMNTOOLS" value="disable" /> -->
</section>
<!-- Use this section to add flags to the compilers and linkers -->
<section id="exes">
<!-- Compiling on windows ... -->
<compiler id="MSVC" if="windows">
<!-- Example adding a build flag -->
<!-- <flag value = "-Ie:/VS8//PlatformSDK/Include"/> -->
</compiler>
<!-- Linking on windows ... -->
<linker id="dll" if="windows">
<!-- Add library paths for windows (DirectX needed for building NME) -->
<!-- <flag value = "-libpath:e:\VS8\DirectXSDK\Lib\x86"/>
<flag value = "-libpath:e:/VS8/PlatformSDK/Lib"/> -->
</linker>
<!-- And for linking executables -->
<linker id="exe" if="windows">
<!-- <flag value = "-libpath:e:/VS8/PlatformSDK/Lib"/> -->
<flag value="-static"/> <!--add this-->
<flag value = "-static-libgcc" /> <!--add this-->
<flag value = "-static-libstdc++" /> <!--add this-->
</linker>
</section>
</xml>

Look for .hxcpp_config.xml, i found mine in ~/.hxcpp_config.xml and edit Look for mingw-toolchain.xml in {path to haxe libraries}/hxcpp/toolchain/ and edit

Then in your build.hxml, add these

-D static_ndll
-D no_shared_libs
-D mingw
-D MINGW_ROOT=/usr/local/cellar/mingw-w64/6.0.0_2/toolchain-x86_64 ## path to your mingw installation
-D HXCPP_MINGW
-D HXCPP_M64
-cp src/
-cpp bin/Test
-D static_ndll
-D no_shared_libs
-D mingw
-D MINGW_ROOT=/usr/local/cellar/mingw-w64/6.0.0_2/toolchain-x86_64 ## path to your mingw installation
-D HXCPP_MINGW
-D HXCPP_M64
<xml>
<!-- MINGW TOOLS -->
<section if="linux_host">
<section if="HXCPP_M64">
<set name="HXCPP_MINGW_EXE" value="x86_64-w64-mingw32-g++" unless="HXCPP_MINGW_EXE" />
<set name="MINGW_ROOT" value="/usr/x86_64-w64-mingw32" unless="MINGW_ROOT" />
</section>
<section unless="HXCPP_M64">
<set name="HXCPP_MINGW_EXE" value="i686-w64-mingw32-g++" unless="HXCPP_MINGW_EXE" />
<set name="MINGW_ROOT" value="/usr/i686-w64-mingw32" unless="MINGW_ROOT" />
</section>
</section>
<!-- <set name="HXCPP_MINGW_EXE" value="g++.exe" unless="HXCPP_MINGW_EXE"/> -->
<set name="HXCPP_MINGW_EXE" value="x86_64-w64-mingw32-g++" unless="HXCPP_MINGW_EXE"/>
<setup name="mingw" />
<path name="${MINGW_ROOT}/bin"/>
<include name="toolchain/gcc-toolchain.xml"/>
<unset name="USE_PRECOMPILED_HEADERS"/>
<set name="HXCPP_CPP11" value="1" unless="HXCPP_NO_CPP11" />
<set name="SUBSYSTEMWINDOWS" value="1" if="no_console" unless="HXCPP_DEBUGGER" />
<compiler id="mingw" exe="gcc">
<exe name="${HXCPP_MINGW_EXE}" />
<flag value="-c"/>
<cppflag value="-frtti"/>
<flag value="-Wno-overflow" />
<cppflag value="-Wno-invalid-offsetof" />
<flag value="-g" if="HXCPP_DEBUG_LINK"/>
<flag value="-O2" unless="debug||O3"/>
<flag value="-O3" if="O3"/>
<include name="toolchain/common-defines.xml" />
<flag value="-DHX_WINDOWS"/>
<flag value="-DHXCPP_BIG_ENDIAN" if="HXCPP_BIG_ENDIAN"/>
<cppflag value="-std=c++11" if="HXCPP_CPP11" />
<flag value="-m32" unless="HXCPP_M64"/>
<flag value="-DHXCPP_M64" if="HXCPP_M64"/>
<flag value="-I${HXCPP}/include"/>
<objdir value="obj/mingw${OBJEXT}/"/>
<outflag value="-o"/>
<ext value=".o"/>
</compiler>
<linker id="dll" exe="g++">
<exe name="${HXCPP_MINGW_EXE}" />
<flag value="-shared"/>
<flag value="-debug" if="debug"/>
<flag value="--enable-auto-import"/>
<flag value="-m32" unless="HXCPP_M64"/>
<flag value="-static" if="no_shared_libs"/>
<flag value="-static-libgcc" if="no_shared_libs"/>
<flag value="-static-libstdc++" if="no_shared_libs"/>
<flag value="-L${MINGW_ROOT}/lib/libs" />
<ext value=".dll"/>
<outflag value="-o "/>
</linker>
<linker id="exe" exe="g++">
<exe name="${HXCPP_MINGW_EXE}" />
<flag value="-debug" if="debug"/>
<flag value="-Wl,--enable-auto-import"/>
<flag value="-mwindows" if="SUBSYSTEMWINDOWS" />
<flag value="-m32" unless="HXCPP_M64"/>
<flag value="-static" if="no_shared_libs"/>
<flag value="-static-libgcc" if="no_shared_libs" unless="linux_host"/>
<flag value="-static-libstdc++" if="no_shared_libs" unless="linux_host"/>
<flag value="-static-libgcc" if="linux_host"/>
<flag value="-static-libstdc++" if="linux_host"/>
<flag value="-L${MINGW_ROOT}/lib/libs" />
<ext value=".exe"/>
<outflag value="-o "/>
</linker>
<copyFile toolId="exe" name="libgcc_s_dw2-1.dll" from="${MINGW_ROOT}/bin" allowMissing="true" unless="no_shared_libs"/>
<copyFile toolId="exe" name="libstdc++-6.dll" from="${MINGW_ROOT}/bin" allowMissing="true" unless="no_shared_libs"/>
<copyFile toolId="exe" name="libwinpthread-1.dll" from="${MINGW_ROOT}/sys-root/mingw/bin" allowMissing="true" unless="no_shared_libs"/>
<copyFile toolId="exe" name="libwinpthread-1.dll" from="${MINGW_ROOT}/lib" allowMissing="true" unless="no_shared_libs"/>
<linker id="static_link" exe="ar" >
<ext value="${LIBEXT}"/>
<exe name="${EXEPREFIX}-ar" if="EXEPREFIX" />
<exe name="${HXCPP_AR}" if="HXCPP_AR" />
<fromfile value="" if="GCC_OLD" />
<outflag value="-cr "/>
<expandAr value="true" unless="HXCPP_USE_LIBTOOL" />
<section if="HXCPP_USE_LIBTOOL">
<exe name="libtool" />
<outflag value="-o " />
<fromfile value="-filelist " />
</section>
<!-- <outflag value="-o " if="emscripten" /> -->
<section unless="HXCPP_USE_LIBTOOL">
<ranlib name="ranlib" />
<ranlib name="${EXEPREFIX}-ranlib" if="EXEPREFIX" />
<ranlib name="emranlib" if="emscripten" />
<ranlib name="${HXCPP_RANLIB}" if="HXCPP_RANLIB" />
</section>
</linker>
</xml>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment