Skip to content

Commit bd8d105

Browse files
Support cross compiling to ARM64 in our build scripts.
1 parent 399c21f commit bd8d105

File tree

6 files changed

+118
-68
lines changed

6 files changed

+118
-68
lines changed

external/buildscripts/Build.bee.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static void RegisterOSXArtifacts()
7575

7676
Artifacts.Add("MacBuildEnvironment",
7777
new Tuple<string, string>(
78-
"MacBuildEnvironment/9df1e3b3b120_2fc8e616a2e5dfb7907fc42d9576b427e692223c266dc3bc305de4bf03714e30.7z",
78+
"mac-toolchain-11_0/12.0-12A8158a_2b346a6c93e9c82250a1d88c02f24a5dea9f0bd1aa2ef44109896a44800e8c68.zip",
7979
"unity-internal"));
8080

8181
Artifacts.Add("mono-build-tools-extra",

external/buildscripts/build.pl

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
my $existingMonoRootPath = '';
5353
my $sdk = '';
5454
my $arch32 = 0;
55+
my $targetArch = "";
5556
my $winPerl = "";
5657
my $winMonoRoot = "";
5758
my $msBuildVersion = "14.0";
@@ -97,6 +98,7 @@
9798
'runtimetests=i'=>\$runRuntimeTests,
9899
'classlibtests=i'=>\$runClasslibTests,
99100
'arch32=i'=>\$arch32,
101+
'targetarch=s'=>\$targetArch,
100102
'jobs=i'=>\$jobs,
101103
'sdk=s'=>\$sdk,
102104
'existingmono=s'=>\$existingMonoRootPath,
@@ -206,10 +208,16 @@
206208
}
207209
elsif($^O eq 'darwin')
208210
{
209-
$monoHostArch = $arch32 ? "i386" : "x86_64";
211+
$monoHostArch = "x86_64";
210212
$existingExternalMono = "$existingExternalMonoRoot";
211213
$existingExternalMonoBinDir = "bin";
212214

215+
if ($targetArch eq "arm64")
216+
{
217+
$disableMcs = 1;
218+
$test = 0;
219+
}
220+
213221
# From Massi: I was getting failures in install_name_tool about space
214222
# for the commands being too small, and adding here things like
215223
# $ENV{LDFLAGS} = '-headerpad_max_install_names' and
@@ -447,22 +455,12 @@
447455
}
448456

449457
my $macSdkPath = "";
450-
my $macversion = '10.8';
458+
my $macversion = '10.12';
451459
my $darwinVersion = "10";
452460
if ($^O eq 'darwin')
453461
{
454-
if ($sdk eq '')
455-
{
456-
$sdk='10.11';
457-
}
458-
459-
my $macBuildEnvDir = "$externalBuildDeps/MacBuildEnvironment";
460-
$macSdkPath = "$macBuildEnvDir/builds/MacOSX$sdk.sdk";
461-
if (! -d $macSdkPath)
462-
{
463-
print(">>> Unzipping mac build toolchain\n");
464-
system("unzip", '-qd', "$macBuildEnvDir", "$macBuildEnvDir/builds.zip") eq 0 or die ("failed unzipping mac build toolchain\n");
465-
}
462+
$sdk='11.0';
463+
$macSdkPath = "$externalBuildDeps/mac-toolchain-11_0/MacOSX$sdk.sdk";
466464
}
467465

468466
if ($iphone || $iphoneSimulator)
@@ -1156,20 +1154,37 @@
11561154
$existingMonoRootPath = "$monoInstalls/$monoVersionToUse";
11571155
}
11581156

1157+
if ($targetArch eq "arm64")
1158+
{
1159+
$macversion = "11.0"; # To build on ARM64, we need to specify min OS version as 11.0 as we need to use new APIs from 11.0
1160+
}
1161+
11591162
$mcs = "EXTERNAL_MCS=$existingMonoRootPath/bin/mcs";
11601163

11611164
$ENV{'CC'} = "$macSdkPath/../usr/bin/clang";
11621165
$ENV{'CXX'} = "$macSdkPath/../usr/bin/clang++";
1163-
$ENV{'CFLAGS'} = $ENV{MACSDKOPTIONS} = "-D_XOPEN_SOURCE -I$macBuildEnvDir/builds/usr/include -mmacosx-version-min=$macversion -isysroot $macSdkPath";
1166+
$ENV{'CFLAGS'} = $ENV{MACSDKOPTIONS} = "-mmacosx-version-min=$macversion -isysroot $macSdkPath -g";
11641167

1165-
$ENV{CFLAGS} = "$ENV{CFLAGS} -g -O0" if $debug;
1168+
$ENV{CFLAGS} = "$ENV{CFLAGS} -O0" if $debug;
11661169
$ENV{CFLAGS} = "$ENV{CFLAGS} -Os" if not $debug; #optimize for size
11671170

1168-
$ENV{CC} = "$ENV{CC} -arch $monoHostArch";
1169-
$ENV{CXX} = "$ENV{CXX} -arch $monoHostArch";
1171+
$ENV{CC} = "$ENV{CC} -arch $targetArch";
1172+
$ENV{CXX} = "$ENV{CXX} -arch $targetArch";
11701173

11711174
# Add OSX specific autogen args
1172-
push @configureparams, "--host=$monoHostArch-apple-darwin12.2.0";
1175+
1176+
if ($targetArch eq "x86_64")
1177+
{
1178+
push @configureparams, "--host=x86_64-apple-darwin12.2.0";
1179+
}
1180+
elsif ($targetArch eq "arm64")
1181+
{
1182+
push @configureparams, "--host=aarch64-apple-darwinmacos12.2.0";
1183+
}
1184+
else
1185+
{
1186+
die("Unsupported macOS architecture: $targetArch");
1187+
}
11731188

11741189
# Need to define because Apple's SIP gets in the way of us telling mono where to find this
11751190
push @configureparams, "--with-libgdiplus=$addtoresultsdistdir/lib/libgdiplus.dylib";
@@ -1507,10 +1522,9 @@
15071522
}
15081523
elsif($^O eq 'darwin')
15091524
{
1510-
# Note these tmp directories will get merged into a single 'osx' directory later by a parent script
1511-
$embedDirArchDestination = "$embedDirRoot/osx-tmp-$monoHostArch";
1512-
$distDirArchBin = "$distdir/bin-osx-tmp-$monoHostArch";
1513-
$versionsOutputFile = $arch32 ? "$buildsroot/versions-osx32.txt" : "$buildsroot/versions-osx64.txt";
1525+
$embedDirArchDestination = "$embedDirRoot/osx-tmp-$targetArch";
1526+
$distDirArchBin = "$distdir/bin-osx-tmp-$targetArch";
1527+
$versionsOutputFile = "$buildsroot/versions-macos-$targetArch.txt";
15141528
}
15151529
else
15161530
{
@@ -1724,4 +1738,4 @@
17241738
print(">>> Skipping unit tests\n");
17251739
}
17261740

1727-
chdir ($currentdir);
1741+
chdir ($currentdir);

external/buildscripts/build_all_osx.pl

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,29 @@
6262
}
6363

6464
print(">>> Building x86_64\n");
65-
system("perl", "$buildscriptsdir/build.pl", "--clean=1", "--classlibtests=0", @passAlongArgs) eq 0 or die ('failing building x86_64');
65+
system("perl", "$buildscriptsdir/build.pl", "--clean=1", "--classlibtests=0", "--targetarch=x86_64", @passAlongArgs) eq 0 or die ('failing building x86_64');
66+
67+
print(">>> Building ARM64\n");
68+
system("perl", "$buildscriptsdir/build.pl", "--clean=1", "--classlibtests=0", "--targetarch=arm64", @passAlongArgs) eq 0 or die ("failing building ARM64");
6669

6770
if ($artifact)
6871
{
6972
print(">>> Moving built binaries to final output directories\n");
70-
# Merge stuff in the embedruntimes directory
71-
my $embedDirRoot = "$buildsroot/embedruntimes";
72-
my $embedDirDestination = "$embedDirRoot/osx";
73-
my $embedDirSource64 = "$embedDirRoot/osx-tmp-x86_64";
74-
75-
system("mkdir -p $embedDirDestination");
76-
77-
if (!(-d $embedDirSource64))
78-
{
79-
die("Expected source directory not found : $embedDirSource64\n");
80-
}
8173

82-
for my $file ('libmonobdwgc-2.0.dylib','libmonosgen-2.0.dylib','libMonoPosixHelper.dylib')
83-
{
84-
print(">>> cp $embedDirSource64/$file $embedDirDestination/$file\n\n");
85-
system ('cp', "$embedDirSource64/$file", "$embedDirDestination/$file");
86-
}
87-
88-
if (not $buildMachine)
89-
{
90-
print(">>> Doing non-build machine stuff...\n");
91-
for my $file ('libmonobdwgc-2.0.dylib','libmonosgen-2.0.dylib','libMonoPosixHelper.dylib')
92-
{
93-
print(">>> Removing $embedDirDestination/$file.dSYM\n");
94-
rmtree ("$embedDirDestination/$file.dSYM");
95-
print(">>> 'dsymutil $embedDirDestination/$file\n");
96-
system ('dsymutil', "$embedDirDestination/$file") eq 0 or warn ("Failed creating $embedDirDestination/$file.dSYM");
97-
}
74+
# Copy stuff in the embedruntimes directory
75+
my $embedDirRoot = "$buildsroot/embedruntimes";
76+
my $embedDirSourceX64 = "$embedDirRoot/osx-tmp-x86_64";
77+
my $embedDirSourceARM64 = "$embedDirRoot/osx-tmp-arm64";
9878

99-
print(">>> Done with non-build machine stuff\n");
100-
}
79+
CopyEmbedRuntimeBinaries($embedDirSourceX64, "$embedDirRoot/osx");
80+
CopyEmbedRuntimeBinaries($embedDirSourceARM64, "$embedDirRoot/osx-arm64");
10181

10282
# Merge stuff in the monodistribution directory
10383
my $distDirRoot = "$buildsroot/monodistribution";
10484
my $distDirDestinationBin = "$buildsroot/monodistribution/bin";
10585
my $distDirDestinationLib = "$buildsroot/monodistribution/lib";
106-
my $distDirSourceBin64 = "$distDirRoot/bin-osx-tmp-x86_64";
86+
my $distDirSourceBinX64 = "$distDirRoot/bin-osx-tmp-x86_64";
87+
my $distDirSourceBinARM64 = "$distDirRoot/bin-osx-tmp-arm64";
10788

10889
# Should always exist because build_all would have put stuff in it, but in some situations
10990
# depending on the options it may not. So create it if it does not exist
@@ -117,27 +98,79 @@
11798
system("mkdir -p $distDirDestinationLib");
11899
}
119100

120-
if (!(-d $distDirSourceBin64))
101+
if (!(-d $distDirSourceBinX64))
102+
{
103+
die("Expected source directory not found : $distDirSourceBinX64\n");
104+
}
105+
106+
if (!(-d $distDirSourceBinARM64))
107+
{
108+
die("Expected source directory not found : $distDirSourceBinARM64\n");
109+
}
110+
111+
for my $file ('mono')
121112
{
122-
die("Expected source directory not found : $distDirSourceBin64\n");
113+
MergeIntoFatBinary("$distDirSourceBinX64/$file", "$distDirSourceBinARM64/$file", "$distDirDestinationBin/$file");
123114
}
124115

125-
for my $file ('mono','pedump')
116+
for my $file ('pedump')
126117
{
127-
system ('mv', "$distDirSourceBin64/$file", "$distDirDestinationBin/$file");
118+
# pedump doens't get cross-compiled
119+
system ('mv', "$distDirSourceBinX64/$file", "$distDirDestinationBin/$file") eq 0 or die ("Failed to move '$distDirSourceBinX64/$file' to '$distDirDestinationBin/$file'.");
128120
}
129121

130122
for my $file ('libMonoPosixHelper.dylib')
131123
{
132-
print(">>> cp $embedDirSource64/$file $distDirDestinationLib/$file\n\n");
133-
system ('cp', "$embedDirSource64/$file", "$distDirDestinationLib/$file");
124+
MergeIntoFatBinary("$embedDirSourceX64/$file", "$embedDirSourceARM64/$file", "$distDirDestinationLib/$file");
134125
}
135126

136127
if ($buildMachine)
137128
{
138129
print(">>> Clean up temporary arch specific build directories\n");
139130

140-
rmtree("$distDirSourceBin64");
141-
rmtree("$embedDirSource64");
131+
rmtree("$distDirSourceBinX64");
132+
rmtree("$distDirSourceBinARM64");
133+
rmtree("$embedDirSourceX64");
134+
rmtree("$embedDirSourceARM64");
142135
}
143136
}
137+
138+
sub CopyEmbedRuntimeBinaries
139+
{
140+
my ($embedDirSource, $embedDirDestination) = @_;
141+
142+
system("mkdir -p $embedDirDestination");
143+
144+
if (!(-d $embedDirSource))
145+
{
146+
die("Expected source directory not found : $embedDirSource\n");
147+
}
148+
149+
for my $file ('libmonobdwgc-2.0.dylib','libmonosgen-2.0.dylib','libMonoPosixHelper.dylib')
150+
{
151+
print(">>> cp $embedDirSource/$file $embedDirDestination/$file\n\n");
152+
system('cp', "$embedDirSource/$file", "$embedDirDestination/$file") eq 0 or die("Failed to copy '$embedDirSource/$file' to '$embedDirDestination/$file'.");
153+
}
154+
155+
if (not $buildMachine)
156+
{
157+
print(">>> Doing non-build machine stuff...\n");
158+
for my $file ('libmonobdwgc-2.0.dylib','libmonosgen-2.0.dylib','libMonoPosixHelper.dylib')
159+
{
160+
print(">>> Removing $embedDirDestination/$file.dSYM\n");
161+
rmtree("$embedDirDestination/$file.dSYM");
162+
print(">>> 'dsymutil $embedDirDestination/$file\n");
163+
system('dsymutil', "$embedDirDestination/$file") eq 0 or warn("Failed creating $embedDirDestination/$file.dSYM");
164+
}
165+
166+
print(">>> Done with non-build machine stuff\n");
167+
}
168+
}
169+
170+
sub MergeIntoFatBinary
171+
{
172+
my ($binary1, $binary2, $binaryOutput) = @_;
173+
174+
print(">>> Merging '$binary1' and '$binary2' into '$binaryOutput'\n\n");
175+
system('lipo', "$binary1", "$binary2", "-create", "-output", "$binaryOutput") eq 0 or die("Failed to run lipo!");
176+
}

external/buildscripts/build_classlibs_osx.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"--build=$build",
3535
"--clean=$clean",
3636
"--mcsonly=$mcsOnly",
37+
"--targetarch=x86_64",
3738
"--skipmonomake=$skipMonoMake",
3839
"--artifact=1",
3940
"--artifactscommon=1",

libgc/gcj_mlc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ int GC_gcj_debug_kind; /* The kind of objects that is always marked */
5050
ptr_t * GC_gcjobjfreelist;
5151
ptr_t * GC_gcjdebugobjfreelist;
5252

53+
void GC_start_debugging();
54+
5355
/* Caller does not hold allocation lock. */
5456
void GC_init_gcj_malloc(int mp_index, void * /* really GC_mark_proc */mp)
5557
{

support/sys-uio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Mono_Posix_Syscall_writev (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcn
8282
}
8383
#endif /* def HAVE_WRITEV */
8484

85-
#ifdef HAVE_PREADV
85+
#if defined(HAVE_PREADV) && !defined(__APPLE__) // Configure incorrectly detects that this function is available on macOS SDK 11.0 (it is not)
8686
gint64
8787
Mono_Posix_Syscall_preadv (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt, gint64 off)
8888
{
@@ -100,9 +100,9 @@ Mono_Posix_Syscall_preadv (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcn
100100
free (v);
101101
return res;
102102
}
103-
#endif /* def HAVE_PREADV */
103+
#endif /* defined(HAVE_PREADV) && !defined(__APPLE__) */
104104

105-
#ifdef HAVE_PWRITEV
105+
#if defined(HAVE_PWRITEV) && !defined(__APPLE__) // Configure incorrectly detects that this function is available on macOS SDK 11.0 (it is not)
106106
gint64
107107
Mono_Posix_Syscall_pwritev (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt, gint64 off)
108108
{
@@ -120,7 +120,7 @@ Mono_Posix_Syscall_pwritev (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovc
120120
free (v);
121121
return res;
122122
}
123-
#endif /* def HAVE_PWRITEV */
123+
#endif /* defined(HAVE_PWRITEV) && !defined(__APPLE__) */
124124

125125

126126
/*

0 commit comments

Comments
 (0)