Skip to content

Commit 1043cf2

Browse files
authored
Add support for HXCPP_ANDROID_PLATFORM define (#1201)
* Add support for the HXCPP_ANDROID_PLATFORM define This also deprecates the PLATFORM_NUMBER define. Also removes notes about frameworks setting minSdkVersion as this was outdated and confusing. * Use ndk metadata for default android platform Instead of hardcoding default values per ndk version, it is better to read the ndk metadata to figure out the minimum version supported by the ndk.
1 parent 14146ef commit 1043cf2

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

docs/build_xml/Defines.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Defines affecting target architecture.
6767
| *HXCPP_LINUX_ARM64* | Run on a linux ARM64 device |
6868
| *winrt* | Compile for windowsRt/windows UWP |
6969
| *android* | Compile for android |
70-
| *PLATFORM* | Specify the android platform for NDK compilation |
70+
| *HXCPP_ANDROID_PLATFORM* | Specify the android platform for NDK compilation |
7171
| *ANDROID_NDK_ROOT* | Specify the location of the android NDK toolchain |
7272
| *ANDROID_NDK_DIR* | Specify the search location for finding the android NDK toolchain |
7373
| *HXCPP_X86* | Compile android for x86 architecture |

toolchain/android-toolchain-clang.xml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
<xml>
2-
2+
33
<!-- Set architecture -->
44
<section if="HXCPP_X86">
55
<set name="ARCH" value="-x86" />
6-
<set name="PLATFORM_NUMBER" value="21" unless="PLATFORM_NUMBER" />
76
<set name="ABITRIPLE" value="i686-linux-android" />
87
</section>
98

109
<section if="HXCPP_X86_64">
1110
<set name="ARCH" value="-x86_64" />
12-
<set name="PLATFORM_NUMBER" value="21" unless="PLATFORM_NUMBER" />
1311
<set name="ABITRIPLE" value="x86_64-linux-android" />
1412
</section>
1513

1614
<section if="HXCPP_ARMV7">
1715
<set name="ARCH" value="-v7" />
18-
<set name="PLATFORM_NUMBER" value="21" unless="PLATFORM_NUMBER" />
1916
<set name="ABITRIPLE" value="armv7a-linux-androideabi" />
2017
<set name="EXEPREFIX" value="arm-linux-androideabi" />
2118
</section>
2219

2320
<section if="HXCPP_ARM64">
2421
<set name="ARCH" value="-64"/>
25-
<set name="PLATFORM_NUMBER" value="21" unless="PLATFORM_NUMBER" />
2622
<set name="ABITRIPLE" value="aarch64-linux-android" />
2723
</section>
2824

2925
<error value="Please set one of the architectures, eg HXCPP_ARM64, HXCPP_X86, ..." unless="ABITRIPLE" />
3026

31-
<set name="PLATFORM" value="android-${PLATFORM_NUMBER}" />
32-
<set name="ANDROID_PLATFORM_DEFINE" value="HXCPP_ANDROID_PLATFORM=${PLATFORM_NUMBER}" />
33-
3427
<path name="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/${ANDROID_HOST}/bin" />
3528

3629
<section unless="NDKV22+">
@@ -47,7 +40,7 @@
4740
<compiler id="android-gcc" exe="clang++">
4841

4942
<exe name="clang++" />
50-
<flag value="--target=${ABITRIPLE}${PLATFORM_NUMBER}" />
43+
<flag value="--target=${ABITRIPLE}${HXCPP_ANDROID_PLATFORM}" />
5144

5245
<!-- File Related -->
5346
<include name="toolchain/common-defines.xml" />
@@ -64,8 +57,8 @@
6457
<flag value="-DHXCPP_CLANG"/>
6558
<flag value="-DHX_ANDROID"/>
6659
<flag value="-DHXCPP_LOAD_DEBUG" if="HXCPP_LOAD_DEBUG"/>
67-
<flag value="-D${ANDROID_PLATFORM_DEFINE}"/>
68-
60+
<flag value="-DHXCPP_ANDROID_PLATFORM=${HXCPP_ANDROID_PLATFORM}" />
61+
6962
<!-- Options -->
7063
<cppflag value="-std=c++17" if="HXCPP_CPP17" />
7164
<flag value="-flto" if="HXCPP_OPTIMIZE_LINK" unless="debug"/>
@@ -90,7 +83,7 @@
9083

9184
<exe name="clang++" />
9285
<flag value ="-shared" />
93-
<flag value="--target=${ABITRIPLE}${PLATFORM_NUMBER}" />
86+
<flag value="--target=${ABITRIPLE}${HXCPP_ANDROID_PLATFORM}" />
9487

9588
<!-- Build time error, not run time -->
9689
<flag value="-Wl,--no-undefined" unless="HXCPP_ALLOW_UNDEFINED" />

toolchain/android-toolchain-gcc.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<flag value="-finline-limit=10000"/>
130130
<flag value="-DANDROID=ANDROID"/>
131131
<flag value="-DHX_ANDROID"/>
132-
<flag value="-D${ANDROID_PLATFORM_DEFINE}"/>
132+
<flag value="-DHXCPP_ANDROID_PLATFORM=${HXCPP_ANDROID_PLATFORM}"/>
133133
<!-- todo <flag value="-Werror"/> -->
134134
<flag value="-Wa,--noexecstack"/>
135135
<flag value="-O2" unless="debug || HXCPP_OPTIMIZE_FOR_SIZE || HXCPP_OPTIMIZE_FOR_FAST"/>

tools/hxcpp/Setup.hx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,36 @@ class Setup
516516
}
517517
catch(e:Dynamic) { }
518518

519-
if(defines.exists('NDKV20+')) {
520-
Log.v([
521-
"x86 Platform: 16",
522-
"arm Platform: 16",
523-
"x86_64 Platform: 21",
524-
"arm_64 Platform: 21",
525-
"Frameworks should set the minSdkVersion for each APK to these values."
526-
].join('\n'));
519+
if (defines.exists('HXCPP_ANDROID_PLATFORM')) {
520+
Log.info("", "\x1b[33;1mUsing Android NDK platform: " + defines.get("HXCPP_ANDROID_PLATFORM") + "\x1b[0m");
521+
}
522+
else if (defines.exists('NDKV20+')) {
523+
if (defines.exists("PLATFORM_NUMBER")) {
524+
Log.warn("The PLATFORM_NUMBER define is deprecated. Please use the HXCPP_ANDROID_PLATFORM define instead.");
525+
defines.set("HXCPP_ANDROID_PLATFORM", Std.string(defines.get("PLATFORM_NUMBER")));
526+
} else {
527+
var platformsJson = root + "/meta/platforms.json";
528+
529+
var minPlatform:Null<Int> = try {
530+
haxe.Json.parse(sys.io.File.getContent(platformsJson)).min;
531+
} catch (e) {
532+
Log.warn("Unable to determine minimum supported Android platform: " + e.toString());
533+
null;
534+
};
535+
536+
if (minPlatform == null) {
537+
Log.warn("Defaulting to Android platform 21");
538+
minPlatform = 21;
539+
}
540+
541+
// only platform version 21 and above support 64 bit
542+
// https://developer.android.com/about/versions/lollipop#Perf
543+
if (minPlatform < 21 && (defines.exists('HXCPP_ARM64') || defines.exists('HXCPP_X86_64'))) {
544+
minPlatform = 21;
545+
}
546+
547+
defines.set("HXCPP_ANDROID_PLATFORM", Std.string(minPlatform));
548+
}
527549
}
528550
else {
529551
globallySetThePlatform(root, defines);
@@ -581,7 +603,7 @@ class Setup
581603
defines.set("PLATFORM", "android-" + best);
582604
androidPlatform = best;
583605
}
584-
defines.set("ANDROID_PLATFORM_DEFINE", "HXCPP_ANDROID_PLATFORM=" + androidPlatform);
606+
defines.set("HXCPP_ANDROID_PLATFORM", Std.string(androidPlatform));
585607
if (Log.verbose) Log.println("");
586608
}
587609

0 commit comments

Comments
 (0)