tpoechtrager / osxcross

Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android (Termux)
GNU General Public License v2.0
2.86k stars 322 forks source link

Compile For OSX 10.12? #103

Closed davidwilcox closed 5 years ago

davidwilcox commented 7 years ago

Hey. I was wondering if there's any hope of getting this to work with the latest XCode version and OSX. The docs mentioned that XCode 7.3 is the latest (I'm using 8.2.1). I noticed the project only has targets up to OSX 10.11 (I'm using 10.12.1). Or if I should just completely uninstall XCode and install 7.3? And revert my OSX down. I noticed the docs said that 7.3 is the latest installed XCode. Is there any hope?

EDIT: It looks like packaging from my mac mostly worked. The only problem is that OSX 10.12 doesn't seem to be supported yet. Would it be hard for me to add support for that version of OSX? It doesn't seem like there's much in build.sh for it (just differs by darwin15).

I added a line to make 10.12* go to darwin15, but I get an error for:

testing o32-clang ... osxcross: error: cannot find Mac OS X SDK (expected in: /home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.11.sdk)
osxcross: error: while detecting target

In those directories, it's created MacOSX10.12.sdk.

EDIT2: I updated to darwin16 instead of darwin15. That one gets a bit further (it finds MacOSX10.12), but it fails to be able to read some of the things created.

osxcross: debug: <-- /usr/lib/llvm-3.6/bin/clang (clang) -target i386-apple-darwin16 -mlinker-version=264.3.102 -isystem /usr/lib/llvm-3.6/bin/../lib/clang/3.6.2/include -isysroot /home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.12.sdk -cxx-isystem /home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1 -cxx-isystem /home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/i386-apple-darwin16 -cxx-isystem /home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/backward -mmacosx-version-min=10.5.0 -arch i386 /home/dawilcox/osxcross/oclang/test.c -O2 -Wall -o test 
osxcross: debug: === time spent in wrapper: 0.199488 ms
ld: unexpected token: !tapi-tbd-v2 file '/home/dawilcox/osxcross/target/bin/../SDK/MacOSX10.12.sdk/usr/lib/libgcc_s.10.5.tbd' for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

EDIT3:

I added the following to osxcross-macports with no change. This is how it seemed like you guys added support for 10.11.


+  10.12* ) OSXVERSION="darwin_14" ;;
+       * ) unsupportedDepTarget ;;
+
``` #
tpoechtrager commented 7 years ago

Hi, thanks for your effort.

Xcode 8 will not be supported in the near future. Apple has decided to make the new TAPIv2 proprietary. The new linker (with reverted TAPIv2 support) will follow soon though.

You can of course compile for 10.12 by using the 10.11 SDK.

davidwilcox commented 7 years ago

Got it. So, I would need to build an SDK on a 10.11 machine and compile against that.

tpoechtrager commented 7 years ago

You don't need a 10.11 machine. Just get Xcode 7.3 and use ./tools/gen_sdk_package.sh. See README.

davidwilcox commented 7 years ago

Thanks for the help. I have a "Hello World" application building with osxcross now! Yay!

One more thing... If I need third party libraries (like boost, libcurl, openssl), what's the best way to install those? I can't use the system installed libraries, can I?

I tried compiling against boost and o64-clang++ can't even find the boost headers when clang++ can. I presume that means that I need to install the headers to a special location. Where is that? Is there a guide somewhere for it? I can't find that anywhere in the docs for this project...

EDIT: I found this issue https://github.com/tpoechtrager/osxcross/issues/60 which seems to explain it pretty clearly, but when I follow the steps there, I get osxcross-conf is required...

EDIT2: From reading the osxcross-macports script, it looks like my problem was that I hadn't added the bin directory to my path. I just did that. Looks like it worked. :)

tpoechtrager commented 7 years ago

Good news. Apple open sourced the TAPI library.

I will add TAPIv2 support to OSXCross within the next few weeks.

Pupskuchen commented 7 years ago

Any updates on that?

tpoechtrager commented 7 years ago

Sorry can't work on OSXCross right now. :(

If you need Mac OS 10.12 support, do the following:

Done.

Pupskuchen commented 7 years ago

I'll try that, thank you!

andreygursky commented 6 years ago

I will add TAPIv2 support to OSXCross within the next few weeks.

Sorry can't work on OSXCross right now. :(

ping

davidwilcox commented 6 years ago

I got around to setting this up and performing the steps that you mentioned. Note that I'm now targeting 10.13 instead of 10.12. I got the 10.13 sdk, put it in the tarballs directory, added 10.13*) TARGET=darwin17; X86_64H_SUPPORTED=1; ;; to build.sh, got to the point where o64-clang++ complains about no tapi, performed the steps you mentioned above (but i had to substitude prefix= where you said path=).

Now I'm getting something else though:

[dawilcox@ip-10-27-11-176.ut1 ~/osxcross-9.0.0/osxcross]$ target/bin/o64-clang++-stdc++ hello.cpp 
ld: library not found for -lstdc++

I'm not sure what to do about this error now.

Note that on this same host, I have an osxcross directory with the 10.11 SDK installed and working just fine.

tpoechtrager commented 6 years ago

@andreygursky:

You are right. I should finally get this done.

@davidwilcox:

I need more details. Please attach the output of OCDEBUG=1 target/bin/o64-clang++-stdc++ hello.cpp.

davidwilcox commented 6 years ago

Here's the output. Thanks for your help. For now, I think that we're actually fine... We just need compilation, not really linking. But, it'd still be great to figure this out:

[dawilcox@ip-10-27-11-176.ut1 ~/osxcross-9.0.0/osxcross]$ OCDEBUG=1 target/bin/o64-clang++-stdc++ hello.cpp -v
osxcross: debug: --> target/bin/o64-clang++-stdc++ hello.cpp -v 
osxcross: debug: <-- /home/dawilcox/build-env/clang-3.6.2-home/bin/clang++ (clang++) -target x86_64-apple-darwin17 -mlinker-version=274.2 -isystem /home/dawilcox/build-env/clang-3.6.2-home/bin/../lib/clang/3.6.2/include -isysroot /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk -stdlib=libstdc++ -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1 -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin17 -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/backward -mmacosx-version-min=10.5.0 -arch x86_64 hello.cpp -v -isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/include -Qunused-arguments -L /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/lib -iframework /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/Library/Frameworks 
osxcross: debug: === time spent in wrapper: 0.236246 ms
clang version 3.6.2 (tags/RELEASE_362/final)
Target: x86_64-apple-darwin17
Thread model: posix
 "/home/dawilcox/build-env/clang-3.6.2-home/bin/clang++" -cc1 -triple x86_64-apple-macosx10.5.0 -emit-obj -mrelax-all -disable-free -main-file-name hello.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.2 -v -dwarf-column-info -resource-dir /home/dawilcox/build-env/clang-3.6.2-home/bin/../lib/clang/3.6.2 -isystem /home/dawilcox/build-env/clang-3.6.2-home/bin/../lib/clang/3.6.2/include -isysroot /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1 -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin17 -cxx-isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/backward -isystem /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/include -iframework /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/Library/Frameworks -stdlib=libstdc++ -fdeprecated-macro -fdebug-compilation-dir /home/dawilcox/osxcross-9.0.0/osxcross -ferror-limit 19 -fmessage-length 149 -stack-protector 1 -mstackrealign -fblocks -fblocks-runtime-optional -fobjc-runtime=macosx-10.5.0 -fobjc-dispatch-method=non-legacy -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/hello-686103.o -x c++ hello.cpp
clang -cc1 version 3.6.2 based upon LLVM 3.6.2 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin17"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.0.0"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.0.0/backward"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/local/include"
ignoring nonexistent directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/Library/Frameworks"
ignoring duplicate directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1"
ignoring duplicate directory "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/backward"
ignoring duplicate directory "/home/dawilcox/build-env/clang-3.6.2-home/bin/../lib/clang/3.6.2/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/dawilcox/build-env/clang-3.6.2-home/bin/../lib/clang/3.6.2/include
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/include
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/Library/Frameworks (framework directory)
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/backward
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include
 /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/x86_64-apple-darwin17-ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.5.0 -syslibroot /home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../SDK/MacOSX10.13.sdk -o a.out -lcrt1.10.5.o -L/home/dawilcox/osxcross-9.0.0/osxcross/target/bin/../macports/pkgs/opt/local/lib /tmp/hello-686103.o -lstdc++ -lSystem -lgcc_s.10.5
ld: library not found for -lstdc++
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
andreygursky commented 6 years ago

@tpoechtrager, great, you're back!

after ping I've tried to follow your suggestions as follows on Debian testing (Buster) using default llvm-3.8:

$ cd PATH_TO_OSX
  # clone osxcross.git and apple-libtapi.git cctools-port.git

$ cd osxcross.git
  # add to build.sh:
  # 10.12*) TARGET=darwin16; X86_64H_SUPPORTED=1; ;;
$ ./build.sh
  # build failed

$ cd ../apple-libtapi.git
$ INSTALLPREFIX=PATH_TO_OSX/osxcross.git/target ./build.sh
$ ./install.sh

$ cd ../cctools-port.git/cctools
$ ./configure --prefix=PATH_TO_OSX/osxcross.git/target \
              --target=x86_64-apple-darwin16 \
              --with-libtapi=PATH_TO_OSX/osxcross.git/target
$ make
$ make install

$ cd ../..

After that I could successfully compile a hello world c++ program:

$ OCDEBUG=1 osxcross.git/target/bin/o64-clang++-stdc++ -Wall -g -v -o test test.cpp 
osxcross: debug: --> osxcross.git/target/bin/o64-clang++-stdc++ -Wall -g -v -o test test.cpp 
osxcross: debug: <-- /usr/lib/llvm-3.8/bin/clang (clang++) -target x86_64-apple-darwin16 -mlinker-version=274.2 -isystem /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include -isysroot PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk -stdlib=libstdc++ -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1 -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin16 -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/backward -mmacosx-version-min=10.5.0 -arch x86_64 -Wno-liblto -Wall -g -v -o test test.cpp 
osxcross: debug: === time spent in wrapper: 0.457247 ms
clang version 3.8.1-24 (tags/RELEASE_381/final)
Target: x86_64-apple-darwin16
Thread model: posix
InstalledDir: /usr/bin
 "/usr/lib/llvm-3.8/bin/clang" -cc1 -triple x86_64-apple-macosx10.5.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.2 -v -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=lldb -resource-dir /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1 -isystem /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include -isysroot PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1 -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin16 -cxx-isystem PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/backward -stdlib=libstdc++ -Wno-liblto -Wall -fdeprecated-macro -fdebug-compilation-dir PATH_TO_OSX -ferror-limit 19 -fmessage-length 166 -stack-protector 1 -fblocks -fblocks-runtime-optional -fobjc-runtime=macosx-10.5.0 -fobjc-dispatch-method=non-legacy -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/test-e971f0.o -x c++ test.cpp
clang -cc1 version 3.8.1 based upon LLVM 3.8.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/x86_64-apple-darwin16"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.0.0"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.0.0/backward"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/local/include"
ignoring nonexistent directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/Library/Frameworks"
ignoring duplicate directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1"
ignoring duplicate directory "PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/backward"
ignoring duplicate directory "/usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/llvm-3.8/bin/../lib/clang/3.8.1/include
 PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1
 PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include/c++/4.2.1/backward
 PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/usr/include
 PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "PATH_TO_OSX/osxcross.git/target/bin/x86_64-apple-darwin16-ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.5.0 -syslibroot PATH_TO_OSX/osxcross.git/target/bin/../SDK/MacOSX10.12.sdk -o test -lcrt1.10.5.o /tmp/test-e971f0.o -lstdc++ -lSystem -lgcc_s.10.5
 "PATH_TO_OSX/osxcross.git/target/bin/x86_64-apple-darwin16-dsymutil" -o test.dSYM test
osxcross: debug: dsymutil: cannot find [osxcross-]llvm-dsymutil in PATH
$ file test
test: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL>

# 9500 bytes

Now I've noticed, that apple-libtapi.git/build.sh has

MACOSX_DEPLOYMENT_TARGET=10.9 

So this should be also adjusted manually?

BTW, after I've added building of llvm-dsymutil in apple-libtapi.git/build.sh, it took much time but failed:

[ 88%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcMachineFunctionInfo.cpp.o
PATH_TO_OSX/apple-libtapi.git/src/apple-llvm/src/lib/Target/Hexagon/RDFLiveness.cpp:231:26: error: default initialization of an object of const type 'const RegisterSet' (aka 'const set<rdf::RegisterRef>') without a user-provided default constructor
static const RegisterSet NoRegs;
                         ^
                               {}
[ 88%] Building CXX object lib/Target/PowerPC/CMakeFiles/LLVMPowerPCCodeGen.dir/PPCTOCRegDeps.cpp.o
1 error generated.
lib/Target/Hexagon/CMakeFiles/LLVMHexagonCodeGen.dir/build.make:1046: recipe for target 'lib/Target/Hexagon/CMakeFiles/LLVMHexagonCodeGen.dir/RDFLiveness.cpp.o' failed
make[3]: *** [lib/Target/Hexagon/CMakeFiles/LLVMHexagonCodeGen.dir/RDFLiveness.cpp.o] Error 1
CMakeFiles/Makefile2:6224: recipe for target 'lib/Target/Hexagon/CMakeFiles/LLVMHexagonCodeGen.dir/all' failed
make[2]: *** [lib/Target/Hexagon/CMakeFiles/LLVMHexagonCodeGen.dir/all] Error 2
make[2]: *** Waiting for unfinished jobs....
[ 88%] Building CXX object lib/Target/PowerPC/CMakeFiles/LLVMPowerPCCodeGen.dir/PPCTLSDynamicCall.cpp.o
[ 88%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcRegisterInfo.cpp.o
[ 88%] Building CXX object lib/Target/PowerPC/CMakeFiles/LLVMPowerPCCodeGen.dir/PPCVSXCopy.cpp.o
[ 88%] Building CXX object lib/Target/PowerPC/CMakeFiles/LLVMPowerPCCodeGen.dir/PPCVSXFMAMutate.cpp.o
[ 89%] Building CXX object lib/Target/PowerPC/CMakeFiles/LLVMPowerPCCodeGen.dir/PPCVSXSwapRemoval.cpp.o
[ 89%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcSubtarget.cpp.o
[ 89%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcTargetMachine.cpp.o
[ 89%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcMCInstLower.cpp.o
[ 89%] Building CXX object lib/Target/Sparc/CMakeFiles/LLVMSparcCodeGen.dir/SparcTargetObjectFile.cpp.o
[ 89%] Linking CXX static library ../../libLLVMPowerPCCodeGen.a
[ 89%] Built target LLVMPowerPCCodeGen
[ 89%] Linking CXX static library ../../libLLVMSparcCodeGen.a
[ 89%] Built target LLVMSparcCodeGen
CMakeFiles/Makefile2:13508: recipe for target 'tools/dsymutil/CMakeFiles/llvm-dsymutil.dir/rule' failed
make[1]: *** [tools/dsymutil/CMakeFiles/llvm-dsymutil.dir/rule] Error 2
Makefile:3863: recipe for target 'llvm-dsymutil' failed
make: *** [llvm-dsymutil] Error 2
elapsed 30 min 28 sec
tpoechtrager commented 6 years ago

Looks good.

Now I've noticed, that apple-libtapi.git/build.sh has

MACOSX_DEPLOYMENT_TARGET=10.9 So this should be also adjusted manually?

That variable is only relevant if you want to build the TAPI library for or on MacOS.

BTW, after I've added building of llvm-dsymutil in apple-libtapi.git/build.sh, it took much time but failed:

Wrong repository. :-) You can use my build_llvm_dsymutil.sh script to build llvm-dsymutil.

tpoechtrager commented 6 years ago

@davidwilcox: Did you build the wrapper correctly? Try following @andreygursky's steps. Otherwise the linker may not be compatible with the new SDK.

jblazquez commented 6 years ago

Confirmed that @andreygursky's steps worked for me for targeting 10.12. Looking forward to the changes to target 10.13. Thanks for the hard work!

jblazquez commented 6 years ago

Turns out that the same steps work for 10.13 as well. I had to switch to the 10.13 SDK because the libc++ headers in 10.12 are not compatible with Clang's C++17 mode. In particular, any attempt to #include <unordered_map> when using -std=c++1z mode will be met with the following error:

$ o64-clang++-libc++ -std=c++1z -o test test.cpp
In file included from MacOSX10.12/usr/include/c++/v1/unordered_map:369:
MacOSX10.12/usr/include/c++/v1/__hash_table:1330:43: fatal error: conflicting types for '__hash_table<_Tp, _Hash, _Equal, _Alloc>'
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
                                          ^
MacOSX10.12/usr/include/c++/v1/__hash_table:963:5: note: previous declaration is here
    __hash_table()
    ^
1 error generated.

On the other hand, SDK 10.13 + clang 5.0.0 + -std=c++17 work beautifully.

These are the steps I followed to get a working osxcross against SDK 10.13 (did it all in a Docker container to have a more repeatable set of steps):

$ docker run --rm -it -v /tmp/osxcross:/tmp/osxcross ubuntu:17.10
$ apt update && apt install make autoconf cmake gcc g++ git gobjc++ libssl-dev libtool libxml2-dev patch python uuid-dev wget vim
$ cd /tmp/osxcross
$ git clone https://github.com/mackyle/xar.git
$ cd xar/xar
$ ./autogen.sh
$ make && make install
$ cd ../..
$ vim build_clang.sh # Optional: Add -DLLVM_LINK_LLVM_DYLIB=ON to cmake command to build smaller LLVM binaries
$ CLANG_VERSION=5.0.0 INSTALLPREFIX=/tmp/osxcross/target ./build_clang.sh
$ cd build/llvm-5.0.0.src/build_stage2
$ make install
$ cd ../../..
$ vim build.sh # Add: 10.13*) TARGET=darwin17; X86_64H_SUPPORTED=1; ;;
$ LD_LIBRARY_PATH=/tmp/osxcross/target/lib ./build.sh
# Wait for it to fail...
$ git clone https://github.com/tpoechtrager/apple-libtapi.git
$ cd apple-libtapi
$ INSTALLPREFIX=/tmp/osxcross/target ./build.sh && ./install.sh
$ cd ..
$ git clone https://github.com/tpoechtrager/cctools-port.git
$ cd cctools-port/cctools/
$ ./configure --prefix=/tmp/osxcross/target --target=x86_64-apple-darwin17 --with-libtapi=/tmp/osxcross/target
$ make && make install
$ cd ../..
$ ./build_llvm_dsymutil.sh 
$ ./build_compiler_rt.sh 
$ mkdir -p /tmp/osxcross/target/lib/clang/5.0.0/include
$ mkdir -p /tmp/osxcross/target/lib/clang/5.0.0/lib/darwin
$ cp -rv /tmp/osxcross/build/compiler-rt/include/sanitizer /tmp/osxcross/target/lib/clang/5.0.0/include
$ cp -v /tmp/osxcross/build/compiler-rt/build/lib/darwin/*.a /tmp/osxcross/target/lib/clang/5.0.0/lib/darwin
$ cp -v /tmp/osxcross/build/compiler-rt/build/lib/darwin/*.dylib /tmp/osxcross/target/lib/clang/5.0.0/lib/darwin

I then did the following to get a cross-compiling iOS toolset:

$ cd cctools-port/usage_examples/ios_toolchain
$ vim build.sh # Add this to the cctools/configure line: --with-llvm-config=/tmp/osxcross/target/bin/llvm-config
$ LLVM_DSYMUTIL=/tmp/osxcross/target/bin/llvm-dsymutil ./build.sh /tmp/iPhoneOS11.0.sdk.tar.gz arm64
tpoechtrager commented 6 years ago

Thanks for the great explanation! However, implementing this properly in osxcross and have it work "almost everywhere" will take a while. But I am on it...

kmatheussen commented 6 years ago

Thank you @jblazquez . I used your recipe to setup for 10.13 and I had no problems.

aambati commented 6 years ago

I have followed the steps listed by @jblazquez , build_clag.sh fails with this error:

make[2]: Leaving directory '/tmp/osxcross/build/llvm-5.0.0.src/build_stage1'
CMakeFiles/Makefile2:19013: recipe for target 'tools/clang/tools/driver/CMakeFiles/clang.dir/rule' failed
make[1]: *** [tools/clang/tools/driver/CMakeFiles/clang.dir/rule] Error 2
make[1]: Leaving directory '/tmp/osxcross/build/llvm-5.0.0.src/build_stage1'
Makefile:5178: recipe for target 'clang' failed
make: *** [clang] Error 2

exiting with abnormal exit code (2)
run 'OCDEBUG=1 ./build_clang.sh' to enable debug messages
if it is happening the first time, then just re-run the script

Any suggestions on how to fix this problem?

tpoechtrager commented 6 years ago

You can skip build_clang.sh if you have a recent version of clang installed on your system. Otherwise I'd need the full log to see what's going on.

TerribleAtAllThisCodingStuff commented 6 years ago

So I'm having the same problems that OP did, but I am brick stupid and cannot figure out how to fix it from this forum post. Can anybody give me the dumbed down rundown of how to fix this problem? Do these fixes work with the current versions of Cataclysm DDA? Or am I boned?

BKPrasanna commented 6 years ago

Thank you @jblazquez your steps helped. Thank you @tpoechtrager for this wonderful project.

Out of topic: Sorry: Not sure where to post this. The project I am working uses a lot of open source project like curl, openssl, sqllite3 etc. Few of the projects are cloned and built and the others are installed using "gem". As I am trying to cross compile for macOS on linux, wanted some suggestions on how I can build / install the OSX variants of the open source libraries on linux machine. Any help in this regard will help a lot.

Thanks and Regards, Prasanna

steeve commented 6 years ago

hey @@tpoechtrager, what happened to TAPIv2 support ? Thanks!!

zelid commented 6 years ago

Following @jblazquez 10.13 instructions I get LD_LIBRARY_PATH=/tmp/osxcross/target/lib OCDEBUG=1 JOBS=$(nproc --all) UNATTENDED=1 SDK_VERSION="10.13" OSX_VERSION_MIN="10.8" ./build.sh on Arch Linux failing with:

+ verbose_cmd create_symlink x86_64-apple-darwin17-wrapper xcrun
+ echo create_symlink x86_64-apple-darwin17-wrapper xcrun
+ eval create_symlink x86_64-apple-darwin17-wrapper xcrun
++ create_symlink x86_64-apple-darwin17-wrapper xcrun
++ ln -sf x86_64-apple-darwin17-wrapper xcrun
+ verbose_cmd create_symlink x86_64-apple-darwin17-wrapper i386-apple-darwin17-xcrun
+ echo create_symlink x86_64-apple-darwin17-wrapper i386-apple-darwin17-xcrun
+ eval create_symlink x86_64-apple-darwin17-wrapper i386-apple-darwin17-xcrun
++ create_symlink x86_64-apple-darwin17-wrapper i386-apple-darwin17-xcrun
++ ln -sf x86_64-apple-darwin17-wrapper i386-apple-darwin17-xcrun
+ verbose_cmd create_symlink x86_64-apple-darwin17-wrapper x86_64-apple-darwin17-xcrun
+ echo create_symlink x86_64-apple-darwin17-wrapper x86_64-apple-darwin17-xcrun
+ eval create_symlink x86_64-apple-darwin17-wrapper x86_64-apple-darwin17-xcrun
++ create_symlink x86_64-apple-darwin17-wrapper x86_64-apple-darwin17-xcrun
++ ln -sf x86_64-apple-darwin17-wrapper x86_64-apple-darwin17-xcrun
+ '[' -n 1 ']'
+ '[' 1 -eq 1 ']'
+ [[ xcrun != gcc* ]]
+ [[ xcrun != g++* ]]
+ [[ xcrun != *gstdc++ ]]
+ verbose_cmd create_symlink x86_64-apple-darwin17-wrapper x86_64h-apple-darwin17-xcrun
+ echo create_symlink x86_64-apple-darwin17-wrapper x86_64h-apple-darwin17-xcrun
+ eval create_symlink x86_64-apple-darwin17-wrapper x86_64h-apple-darwin17-xcrun
++ create_symlink x86_64-apple-darwin17-wrapper x86_64h-apple-darwin17-xcrun
++ ln -sf x86_64-apple-darwin17-wrapper x86_64h-apple-darwin17-xcrun
+ '[' 2 -ge 2 ']'
+ '[' 1 -eq 2 ']'
+ popd
+ popd
+ echo ''

++ osxcross-cmp 10.13 '<' 10.8
+ '[' 0 -eq 1 ']'
++ osxcross-cmp 10.8 '<' 10.4
+ '[' 0 -eq 1 ']'
+ unset MACOSX_DEPLOYMENT_TARGET
+ test_compiler o32-clang /tmp/osxcross/oclang/test.c
+ echo -ne 'testing o32-clang ... '
testing o32-clang ... + o32-clang /tmp/osxcross/oclang/test.c -O2 -Wall -o test
osxcross: debug: --> o32-clang /tmp/osxcross/oclang/test.c -O2 -Wall -o test 
osxcross: debug: <-- /usr/bin/clang-6.0 (clang) -target i386-apple-darwin17 -mlinker-version=274.2 -isystem /usr/bin/../lib/clang/6.0.0/include -isysroot /tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk -cxx-isystem /tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1 -cxx-isystem /tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/i386-apple-darwin17 -cxx-isystem /tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/include/c++/4.2.1/backward -mmacosx-version-min=10.8.0 -arch i386 -Wno-liblto /tmp/osxcross/oclang/test.c -O2 -Wall -o test 
osxcross: debug: === time spent in wrapper: 0.332449 ms
ld: unexpected token: !tapi-tbd-v3 file '/tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/lib/libSystem.tbd' for architecture i386
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
+ _exit
+ EC=1
+ '[' 1 -ne 0 ']'
+ test -z build.sh
+ echo ''

+ echo 'exiting with abnormal exit code (1)'
exiting with abnormal exit code (1)
+ test -n 1
+ declare -f -F remove_locks
+ echo 'removing stale locks...'
removing stale locks...
+ remove_locks
+ rm -rf /tmp/osxcross/build/have_cctools_8e9c3f2_0
+ echo 'if it is happening the first time, then just re-run the script'
if it is happening the first time, then just re-run the script
+ echo ''

+ test build.sh = build.sh
+ check_for_bug_1242300
+ '[' -e /etc/issue ']'
++ grep -i ubuntu.13.10 /etc/issue
+ '[' '' ']'

apple-libtapi builds and installs fine.

./build_compiler_rt.sh also fails with:

HEAD is now at 6fa838ae1 Another shot at fixing android r335644 failure
Removing .clone_complete
Removing build/
Already on 'master'
Your branch is up to date with 'origin/master'.
warning: redirecting to https://git.llvm.org/git/compiler-rt.git/
Already up to date.
-- The C compiler identification is Clang 6.0.0
-- The CXX compiler identification is Clang 6.0.0
-- The ASM compiler identification is Clang
-- Found assembler: /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang
-- Check for working C compiler: /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang
-- Check for working C compiler: /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang -- broken
CMake Error at /usr/share/cmake-3.11/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "/tmp/osxcross/target/bin/x86_64-apple-darwin17-clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/sbin/make" "cmTC_6d9dc/fast"
    /usr/sbin/make -f CMakeFiles/cmTC_6d9dc.dir/build.make CMakeFiles/cmTC_6d9dc.dir/build
    make[1]: Entering directory '/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o
    /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang   -isysroot /tmp/osxcross/target/SDK/MacOSX10.13.sdk -mmacosx-version-min=10.8   -o CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o   -c /tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp/testCCompiler.c
    Linking C executable cmTC_6d9dc
    /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6d9dc.dir/link.txt --verbose=1
    /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang   -isysroot /tmp/osxcross/target/SDK/MacOSX10.13.sdk -mmacosx-version-min=10.8 -Wl,-headerpad_max_install_names   CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o  -o cmTC_6d9dc 
    ld: unexpected token: !tapi-tbd-v3 file '/tmp/osxcross/target/SDK/MacOSX10.13.sdk/usr/lib/libSystem.tbd' for architecture x86_64
    clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
    make[1]: *** [CMakeFiles/cmTC_6d9dc.dir/build.make:87: cmTC_6d9dc] Error 1
    make[1]: Leaving directory '/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp'
    make: *** [Makefile:126: cmTC_6d9dc/fast] Error 2

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:10 (project)

-- Configuring incomplete, errors occurred!
See also "/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeOutput.log".
See also "/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeError.log".

$ cat /tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeError.log

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang 
Build flags: 
Id flags:  

The output was:
1
ld: unexpected token: !tapi-tbd-v3 file '/tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/lib/libSystem.tbd' for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: /tmp/osxcross/target/bin/x86_64-apple-darwin17-clang++ 
Build flags: 
Id flags:  

The output was:
1
ld: unexpected token: !tapi-tbd-v3 file '/tmp/osxcross/target/bin/../SDK/MacOSX10.13.sdk/usr/lib/libSystem.tbd' for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

Determining if the C compiler works failed with the following output:
Change Dir: /tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/sbin/make" "cmTC_6d9dc/fast"
/usr/sbin/make -f CMakeFiles/cmTC_6d9dc.dir/build.make CMakeFiles/cmTC_6d9dc.dir/build
make[1]: Entering directory '/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o
/tmp/osxcross/target/bin/x86_64-apple-darwin17-clang   -isysroot /tmp/osxcross/target/SDK/MacOSX10.13.sdk -mmacosx-version-min=10.8   -o CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o   -c /tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_6d9dc
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6d9dc.dir/link.txt --verbose=1
/tmp/osxcross/target/bin/x86_64-apple-darwin17-clang   -isysroot /tmp/osxcross/target/SDK/MacOSX10.13.sdk -mmacosx-version-min=10.8 -Wl,-headerpad_max_install_names   CMakeFiles/cmTC_6d9dc.dir/testCCompiler.c.o  -o cmTC_6d9dc 
ld: unexpected token: !tapi-tbd-v3 file '/tmp/osxcross/target/SDK/MacOSX10.13.sdk/usr/lib/libSystem.tbd' for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [CMakeFiles/cmTC_6d9dc.dir/build.make:87: cmTC_6d9dc] Error 1
make[1]: Leaving directory '/tmp/osxcross/build/compiler-rt/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:126: cmTC_6d9dc/fast] Error 2

Any ideas what is wrong and how to solve it?

jblazquez commented 6 years ago

@zelid, I got the same problem recently while following my own instructions, and it turns out that the libtapi library that Thomas uses only supports up to TAPI v2, whereas the recent macOS SDKs use TAPI v3 .tbd files (you can check by opening them in a text editor), which only the most recent TAPI Iibrary open-sourced by a random Apple engineer can read.

Unfortunately I found it very hard to use that newest TAPI library with osxcross, since it depends on an unspecified version of Clang (tried 6.0.1 and it didn't build against it; had to checkout some random commit between Clang 5 and 6) and I had to hack the CMake build scripts to make it even build on Linux (it wants to be built on Mac only). Even then, I didn't get it to work after a few hours of messing around with it. Maybe at some point I'll come back to it because I want to be able to target the latest SDKs as well.

Re-reading my comment from October, I actually have no idea how I made it work back then, but I swear I got it to work with the MacOSX10.13 SDK that I got at the time. I even have a copy of it still and it's definitely all TAPI v2. It's possible that in the meantime Apple updated the 10.13 SDK in recent Xcode versions and switched all .tbd files to TAPI v3. You may want to try downloading Xcode 9.1 or so to get the TAPI v2 10.13 SDK.

tpoechtrager commented 6 years ago

https://github.com/tpoechtrager/apple-libtapi/tree/2.0.0

I haven't tested the resulting library in any way. However it would be great if someone could test it. Thanks.

(Ignore the failed travis build it's due to an old version of GCC)

jblazquez commented 6 years ago

Thanks @tpoechtrager! I was finally able to get osxcross to work with Clang 5.0.2 and MacOS SDK 10.13.

First, for some reason the new libtapi doesn't actually enable v3 support. I sent you a PR for it: https://github.com/tpoechtrager/apple-libtapi/pull/7

Once that pull request is merged into the 2.0.0 branch, you can build a cross-compiling toolset that works against 10.13 like this. The following steps assume you have copied the MacOS10.13 SDK to /tmp/MacOSX10.13.sdk.tar.xz:

$ docker run --rm -it -v /tmp:/tmp ubuntu:17.10
$ apt update && apt install -y make autoconf clang-5.0 cmake gcc g++ git gobjc++ libssl-dev libtool libxml2-dev patch python uuid-dev wget vim
$ ln -s /usr/bin/clang-5.0 /usr/bin/clang && ln -s /usr/bin/clang++-5.0 /usr/bin/clang++
$ cd /tmp && git clone https://github.com/tpoechtrager/osxcross.git
$ cd /tmp/osxcross && git clone https://github.com/mackyle/xar.git
$ cd xar/xar && ./autogen.sh --prefix=/tmp/osxcross/target && make -j8 && make -j8 install && cd ../..
$ sed -i -e 's/\*) echo "Invalid SDK Version"/10.13*) TARGET=darwin17; X86_64H_SUPPORTED=1; ;; *) echo "Invalid SDK Version"/g' build.sh
$ cp /tmp/MacOSX10.13.sdk.tar.xz /tmp/osxcross/tarballs/
$ LD_LIBRARY_PATH=/tmp/osxcross/target/lib JOBS=8 ./build.sh
# Wait for it to fail...
$ git clone https://github.com/tpoechtrager/apple-libtapi.git && cd apple-libtapi && git checkout 2.0.0
$ INSTALLPREFIX=/tmp/osxcross/target JOBS=8 ./build.sh && ./install.sh && cd ..
$ git clone https://github.com/tpoechtrager/cctools-port.git
$ cd cctools-port/cctools && ./configure --prefix=/tmp/osxcross/target --target=x86_64-apple-darwin17 --with-libtapi=/tmp/osxcross/target && make -j8 && make -j8 install && cd ../..

Once that's done, you can try building a sample app:

#include <unordered_map>

int main() {}

Save that to /tmp/test.cpp and build it like this:

/tmp/osxcross/target/bin/o64-clang++-libc++ -o /tmp/test /tmp/test

And you'll get a Mach-O executable:

$ file /tmp/test
/tmp/test: Mach-O 64-bit x86_64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>

I'm gonna see if similar steps work for Clang 6.0.1, MacOS 10.14 and iOS 12.0.

steeve commented 6 years ago

This looks great guys!

jblazquez commented 6 years ago

Here's how you can build a cross-compiling toolset with Clang 6.0.1 by building Clang from source. Again, it assumes you have copied the MacOS10.13 SDK to /tmp/MacOSX10.13.sdk.tar.xz:

$ docker run --rm -it -v /tmp:/tmp ubuntu:17.10
$ apt update && apt install -y make autoconf cmake gcc g++ git gobjc++ libssl-dev libtool libxml2-dev patch python uuid-dev wget vim
$ cd /tmp && git clone https://github.com/tpoechtrager/osxcross.git
$ cd /tmp/osxcross && git clone https://github.com/mackyle/xar.git
$ cd xar/xar && ./autogen.sh --prefix=/tmp/osxcross/target && make -j8 && make -j8 install && cd ../..
$ CLANG_VERSION=6.0.1 INSTALLPREFIX=/tmp/osxcross/target JOBS=8 ./build_clang.sh
$ cd build/llvm-6.0.1.src/build_stage2 && make install && cd ../../..
$ sed -i -e 's/\*) echo "Invalid SDK Version"/10.13*) TARGET=darwin17; X86_64H_SUPPORTED=1; ;; *) echo "Invalid SDK Version"/g' build.sh
$ cp /tmp/MacOSX10.13.sdk.tar.xz /tmp/osxcross/tarballs/
$ LD_LIBRARY_PATH=/tmp/osxcross/target/lib JOBS=8 ./build.sh
# Wait for it to fail...
$ git clone https://github.com/tpoechtrager/apple-libtapi.git && cd apple-libtapi && git checkout 2.0.0
$ INSTALLPREFIX=/tmp/osxcross/target JOBS=8 ./build.sh && ./install.sh && cd ..
$ git clone https://github.com/tpoechtrager/cctools-port.git
$ cd cctools-port/cctools && ./configure --prefix=/tmp/osxcross/target --target=x86_64-apple-darwin17 --with-libtapi=/tmp/osxcross/target && make -j8 && make -j8 install && cd ../..
$ JOBS=8 ./build_llvm_dsymutil.sh 
$ JOBS=8 ./build_compiler_rt.sh 
$ mkdir -p /tmp/osxcross/target/lib/clang/6.0.1/include
$ mkdir -p /tmp/osxcross/target/lib/clang/6.0.1/lib/darwin
$ cp -rv /tmp/osxcross/build/compiler-rt/include/sanitizer /tmp/osxcross/target/lib/clang/6.0.1/include
$ cp -v /tmp/osxcross/build/compiler-rt/build/lib/darwin/*.a /tmp/osxcross/target/lib/clang/6.0.1/lib/darwin
$ cp -v /tmp/osxcross/build/compiler-rt/build/lib/darwin/*.dylib /tmp/osxcross/target/lib/clang/6.0.1/lib/darwin

You can then build a sample app like this:

PATH=/tmp/osxcross/target/bin o64-clang++-libc++ -o /tmp/test /tmp/test.cpp
jblazquez commented 6 years ago

I can confirm that my steps above work with the MacOS10.14 SDK as well. All you need to do is add the following line to build.sh:

10.14*) TARGET=darwin18; X86_64H_SUPPORTED=1; ;;

Everything else remains the same.

As for iPhoneOS12.0, I was able to build a toolchain but the linker crashes. Maybe someone can help figure out what's wrong?

First save this patch to /tmp/build.patch:

--- old/build.sh    2018-06-29 00:51:00.986645618 +0000
+++ new/build.sh    2018-06-29 01:19:54.657658170 +0000
@@ -59,7 +59,7 @@
     fi

     if [ ! -d $directory ]; then
-        git clone $url --depth 1
+        git clone $url
     fi

     pushd $directory &>/dev/null
@@ -79,7 +79,7 @@
     exit 1
 fi

-TRIPLE="arm-apple-darwin11"
+TRIPLE="arm-apple-darwin12"
 TARGETDIR="$PWD/target"
 SDKDIR="$TARGETDIR/SDK"

@@ -129,7 +129,7 @@
 if [ $? -eq 0 ]; then
     case $($LLVM_DSYMUTIL --version | \
            grep "LLVM version" | head -1 | awk '{print $3}') in
-        3.8*|3.9*|4.0*|5.0*) OK=1 ;;
+        3.8*|3.9*|4.0*|5.0*|6.0*) OK=1 ;;
     esac
 fi
 set -e
@@ -172,7 +172,7 @@
 echo ""

 pushd tmp &>/dev/null
-git_clone_repository https://github.com/tpoechtrager/apple-libtapi.git master
+git_clone_repository https://github.com/tpoechtrager/apple-libtapi.git 2.0.0
 pushd apple-libtapi &>/dev/null
 INSTALLPREFIX=$TARGETDIR ./build.sh
 ./install.sh
@@ -191,7 +191,7 @@
 pushd tmp &>/dev/null
 mkdir -p cctools
 pushd cctools &>/dev/null
-../../../../cctools/configure --target=$TRIPLE --prefix=$TARGETDIR --with-libtapi=$TARGETDIR
+../../../../cctools/configure --target=$TRIPLE --prefix=$TARGETDIR --with-libtapi=$TARGETDIR --with-llvm-config=$TARGETDIR/bin/llvm-config
 make -j$JOBS && make install
 popd &>/dev/null
 popd &>/dev/null

Then you can build the toolset like this right after completing the steps from my previous comment. This assumes that you have copied iPhoneOS12.0.sdk.tar.gz to /tmp/osxcross/tarballs/iPhoneOS12.0.sdk.tar.gz:

$ cd cctools-port/usage_examples/ios_toolchain
$ patch -p1 </tmp/build.patch
$ LLVM_DSYMUTIL=/tmp/osxcross/target/bin/llvm-dsymutil JOBS=8 ./build.sh /tmp/osxcross/tarballs/iPhoneOS12.0.sdk.tar.gz arm64
$ cp -R target/* /tmp/osxcross/target/

Now you can invoke the iOS compiler (I'm sure there's an easier way than this):

PATH=/tmp/osxcross/target/bin arm-apple-darwin12-clang++ \
-target arm-apple-darwin12 -mlinker-version=274.2 \
-isystem /tmp/osxcross/target/bin/../lib/clang/6.0.1/include \
-isysroot /tmp/osxcross/target/bin/../SDK/iPhoneOS12.0.sdk \
-stdlib=libc++ -nostdinc++ -Qunused-arguments \
-cxx-isystem /tmp/osxcross/target/bin/../SDK/iPhoneOS12.0.sdk/usr/include/c++ \
-miphoneos-version-min=9.0 -arch arm64 -Wno-liblto -o /tmp/test /tmp/test.cpp

Unfortunately the linker crashes:

/tmp/osxcross/cctools-port/usage_examples/ios_toolchain# "/tmp/osxcross/target/bin/arm-apple-darwin12-ld" -demangle -lto_library /tmp/osxcross/target/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -iphoneos_version_min 9.0.0 -syslibroot /tmp/osxcross/target/bin/../SDK/iPhoneOS12.0.sdk -o /tmp/test /tmp/test.o -lc++ -lSystem -v
@(#)PROGRAM:ld  PROJECT:ld64-274.2
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
    /tmp/osxcross/target/bin/../SDK/iPhoneOS12.0.sdk/usr/lib
Framework search paths:
    /tmp/osxcross/target/bin/../SDK/iPhoneOS12.0.sdk/System/Library/Frameworks/
Segmentation fault (core dumped)
tpoechtrager commented 6 years ago

Unfortunately the linker crashes:

Did you inspect the core dump with gdb?

jblazquez commented 6 years ago

I don't have time to debug this further, but here's the crash:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff77750b5 in tapi::v1::LinkerInterfaceFile::shouldPreferTextBasedStubFile (path=...)
    at /tmp/osxcross/cctools-port/usage_examples/ios_toolchain/tmp/apple-libtapi/src/llvm/projects/libtapi/tools/libtapi/LinkerInterfaceFile.cpp:216
216   if (!file) {
(gdb) bt
#0  0x00007ffff77750b5 in tapi::v1::LinkerInterfaceFile::shouldPreferTextBasedStubFile (path=...)
    at /tmp/osxcross/cctools-port/usage_examples/ios_toolchain/tmp/apple-libtapi/src/llvm/projects/libtapi/tools/libtapi/LinkerInterfaceFile.cpp:216
#1  0x000055555559be40 in Options::findFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, Options::FileInfo&) const ()
#2  0x000055555559d7d1 in Options::findLibrary(char const*, bool) const ()
#3  0x00005555555a930b in Options::parse(int, char const**) ()
#4  0x00005555555afade in Options::Options(int, char const**) ()
#5  0x0000555555583c19 in main ()
(gdb) frame 0
#0  0x00007ffff77750b5 in tapi::v1::LinkerInterfaceFile::shouldPreferTextBasedStubFile (path=...)
    at /tmp/osxcross/cctools-port/usage_examples/ios_toolchain/tmp/apple-libtapi/src/llvm/projects/libtapi/tools/libtapi/LinkerInterfaceFile.cpp:216
216   if (!file) {
(gdb) info locals
errorOr = {static isRef = false, {TStorage = {<llvm::AlignedCharArray<8, 8>> = {
        buffer = "\000\000\000\000\000\000\000"}, <No data fields>}, 
    ErrorStorage = {<llvm::AlignedCharArray<8, 16>> = {
        buffer = "\000\000\000\000\000\000\000\000L%G\367\377\177\000"}, <No data fields>}}, HasError = false}
file = {static isRef = false, {TStorage = {<llvm::AlignedCharArray<8, 8>> = {
        buffer = "\000\000\000\000\000\000\000"}, <No data fields>}, 
    ErrorStorage = {<llvm::AlignedCharArray<8, 8>> = {
        buffer = "\000\000\000\000\000\000\000"}, <No data fields>}}, 
  HasError = false}
tpoechtrager commented 6 years ago

Thank you!

Looking into it.

donbright commented 6 years ago

@jblazquez

just wanted to confirm your instructions for clang 6.0.1 on ubuntu 18.04 using 10.13 SDK, I made a few basic changes to get it working.

zeroth was that clang gives a warning on build when other clang is detected so to be safe I did apt purge clang

first is that on ubuntu 18 I had to install libssl1.0-dev which is different from libssl-dev and there are other projects dealing with this issue as well, https://github.com/EmpireProject/Empire/issues/720

the second was that I had to export PATH=/tmp/osxcross/target/bin:$PATH somewhere in there... I think after building clang?

I tested with o64-clang++-libc++ -o /tmp/test /tmp/test.m -framework Cocoa using Guilherm Lampert's http://glampert.com/2012/11-29/osx-window-without-xcode-and-ib/

steeve commented 6 years ago

hey folks, any news on the segfault with the iOS SDK ?

hopefully apple will release the new libtapi on the next open source dump (perhaps with Mojave & Xcode 10 ?)

zllak commented 6 years ago

I've done some digging on this SEGFAULT. There are several problems. But in general, it seems that either libtapi v2.0.0 is not the final one, and even, it seems to be meant to run with a version of llvm that is not yet released, or not available. As an example, the MachODylibReader.cpp is completely commented out, because it includes a llvm/ObjCMetadata/ObjCMachOBinary.h that does not exists in the embedded llvm version. This file is present in the clang 800.0.42.1 which is the latest version released by Apple (https://opensource.apple.com/source/clang/), but is far from being the version used in a modern Xcode (https://trac.macports.org/wiki/XcodeVersionInfo#macOS10.12). The current version builds, but fails to run, I think mainly because there is no implementation for the MachO dylib reader. I think, for now, it's doomed to fail until Apple decides to release both libtapi to the appropriate version, alongside the appropriate version of llvm.

jblazquez commented 6 years ago

Thanks for looking into it @zllak! It's a shame how bad the state of that libtapi 2.0.0 library is.

I'm wondering if it would be possible to backport the stub v3 implementation to the older libtapi?

zllak commented 6 years ago

You're right. The best way to have this working is to wait for Apple to release libtapi 2.0.0, otherwise it will always be a guessing game with the "leaked version" :/ Sad state of the opensource from Apple :/

nehaljwani commented 5 years ago

xref: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=861716fce0066319fce54243dab1aa805e04aeff

cschol commented 5 years ago

Thank you @jblazquez! I was able to build 6.0.1 with 10.13 SDK using your instructions with 1 issue:

$ JOBS=8 ./build_compiler_rt.sh 

The script clones the latest version of compiler_rt, which, with the current version, causes some buffer overflow error when trying to link.

I changed the build_compiler_rt.sh script to check out a revision of the repository from June 28 (the date of the original post of the instructions):

Add this to line 32 of build_compiler_rt.sh:

git checkout 10edf2e95ebca01a2c8442ee29e5f2d942f382d0

Then compiler_rt builds successfully.

EDIT: Compile issues were due to an incorrectly packaged SDK. Everything works as expected now.

tpoechtrager commented 5 years ago

Your SDK does not contain libc++ headers, means it wasn't properly packaged.

https://github.com/tpoechtrager/osxcross#packaging-the-sdk

cschol commented 5 years ago

@tpoechtrager Indeed. Something went wrong during packaging of the SDK. I reran the script and the resulting tarball was larger.

I remember a strange message during the first run, something like "invoking some magic to find the SDK". I might have botched the path passed via XCODEDIR environment variable. I repackaged and extracted to target/SDK directory. Now it works.

Thank you for your help and for this project in general!

martin31821 commented 5 years ago

I managed to build a working toolchain for 10.14, in an ubuntu 18.0 container like this:

FROM ubuntu:18.04

# Install CLang 7.0.1 binary from llvm mirrors
# First somebasic tools
RUN apt update && apt install -yq curl git wget build-essential xz-utils ninja-build nano software-properties-common && rm -rf /var/lib/apt/lists/*
RUN add-apt-repository -y ppa:andrew-fuller/cmake && apt update && apt install -yq cmake

# Then the LLVM repo
RUN echo 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-7 main' >> /etc/apt/sources.list
# LLVM Signing key
RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
# LLVM, Clang
RUN apt update && apt install -yq llvm-7 lld-7 clang-7 clang-tools-7 && rm -rf /var/lib/apt/lists/* && \
    ln -s /usr/bin/clang-7 /usr/bin/clang && \
    ln -s /usr/bin/clang++-7 /usr/bin/clang++ && \
    ln -s /usr/bin/lld-7 /usr/bin/lld && \
    ln -s /usr/bin/llvm-config-7 /usr/bin/llvm-config

# ----------------------------------
# MacOS Cross Toolchain
# ----------------------------------

# Cache busting, due to git
ARG CACHE_BUSTER=okay

# Clone the 10.14 fork of the osxcross toolchain
RUN git clone https://github.com/martin31821/osxcross.git /osxcross
# Add the prepackaged SDK to the container
COPY MacOSX10.14.sdk.tar.xz /osxcross/tarballs/MacOSX10.14.sdk.tar.xz

# Clone and build libxar
RUN apt update && apt install -yq automake libxml2-dev libtool libssl1.0-dev && rm -rf /var/lib/apt/lists/* && git clone https://github.com/mackyle/xar.git /tmp/xar && cd /tmp/xar/xar && ./autogen.sh --prefix=/osxcross/target && make -j8 && make install && ./autogen.sh --prefix=/usr && make -j8 && make install && cd / && rm -rf /tmp/xar

# Make libxar known to the ld64 and cctools build
ENV LD_LIBRARY_PATH=/osxcross/target/lib

# Clone and build libtapi
RUN git clone https://github.com/martin31821/apple-libtapi.git -b 2.0.0 /tmp/tapi && \
    cd /tmp/tapi && INSTALLPREFIX=/osxcross/target JOBS=8 ./build.sh && ./install.sh && cd / && rm -rf /tmp/tapi

# Clone and build recent cctools 
RUN apt update && apt install -yq uuid-dev && rm -rf /var/lib/apt/lists/* && \
    git clone https://github.com/martin31821/cctools-port.git -b cctools-921-ld64-409.12 /tmp/cctools && \
    cd /tmp/cctools/cctools && ./configure --prefix=/osxcross/target --target=x86_64-apple-darwin18 --with-libtapi=/osxcross/target && make -j8 && make install && cd / && rm -rf /tmp/cctools

# Build the toolchain itself, expecting a failure while testing compilers
RUN cd /osxcross && OSX_VERSION_MIN=10.10 UNATTENDED=1 LD_LIBRARY_PATH=/osxcross/target/lib ./build.sh

# Build a test CXX 11 program to verify the toolchain
RUN cd /osxcross/ && ./target/bin/o64-clang++-libc++ -o osxcross oclang/test_libcxx.cpp && rm osxcross
ADD osx.cmake /osx.cmake

Edit: With the 10.14 SDK libstdc++ is not available anymore

tiborvass commented 5 years ago

@martin31821 Thank you for your help. Unfortunately I'm hitting the following blocker with the latest Xcode 10.2 downloaded from Apple.

root@e3385e3d233f:/osxcross# ./target/bin/o64-clang++-libc++ -o osxcross oclang/test_libcxx.cpp
ld: malformed file
/osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/lib/libSystem.tbd:4:18: error: unknown enumerated scalar
platform:        zippered
                 ^~~~~~~~
 file '/osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/lib/libSystem.tbd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So I ran it again with -v:

root@e3385e3d233f:/osxcross# ./target/bin/o64-clang++-libc++ -o osxcross oclang/test_libcxx.cpp -v
clang version 7.0.1-svn348686-1~exp1~20190113235231.54 (branches/release_70)
Target: x86_64-apple-darwin18
Thread model: posix
InstalledDir: /usr/bin
 "/usr/lib/llvm-7/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test_libcxx.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -faligned-alloc-unavailable -target-cpu core2 -dwarf-column-info -debugger-tuning=lldb -target-linker-version 274.2 -v -nostdinc++ -resource-dir /usr/lib/llvm-7/lib/clang/7.0.1 -isystem /usr/lib/llvm-7/bin/../lib/clang/7.0.1/include -isysroot /osxcross/target/bin/../SDK/MacOSX10.14.sdk -cxx-isystem /osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/include/c++ -stdlib=libc++ -Wno-liblto -fdeprecated-macro -fdebug-compilation-dir /osxcross -ferror-limit 19 -fmessage-length 278 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.7.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/test_libcxx-9b81f5.o -x c++ oclang/test_libcxx.cpp
clang -cc1 version 7.0.1 based upon LLVM 7.0.1 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/osxcross/target/bin/../SDK/MacOSX10.14.sdk/Library/Frameworks"
ignoring duplicate directory "/usr/lib/llvm-7/bin/../lib/clang/7.0.1/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/llvm-7/bin/../lib/clang/7.0.1/include
 /osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/include/c++
 /osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/include
 /osxcross/target/bin/../SDK/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/osxcross/target/bin/x86_64-apple-darwin18-ld" -demangle -lto_library /usr/lib/llvm-7/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.7.0 -syslibroot /osxcross/target/bin/../SDK/MacOSX10.14.sdk -o osxcross -lcrt1.10.6.o /tmp/test_libcxx-9b81f5.o -lc++ -lSystem
ld: malformed file
/osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/lib/libSystem.tbd:4:18: error: unknown enumerated scalar
platform:        zippered
                 ^~~~~~~~
 file '/osxcross/target/bin/../SDK/MacOSX10.14.sdk/usr/lib/libSystem.tbd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
tiborvass commented 5 years ago

FWIW, I got @martin31821's Dockerfile to work by replacing "zippered" with "macosx" by doing:

 # Build the toolchain itself, expecting a failure while testing compilers
 RUN cd /osxcross && OSX_VERSION_MIN=10.10 UNATTENDED=1 
 LD_LIBRARY_PATH=/osxcross/target/lib ./build.sh

+RUN find /osxcross/target/ -name '*.tbd' -exec sed -i'' s/zippered/macosx/ {} \;
+
 # Build a test CXX 11 program to verify the toolchain
 RUN cd /osxcross/ && ./target/bin/o64-clang++-libc++ -o osxcross oclang/test_libcxx.cpp && rm osxcross

We probably need a cleaner patch in libtapi v3's code.

steeve commented 5 years ago

Wait, you got libtapi to work ?

tiborvass commented 5 years ago

@steeve I don't know, you tell me :) Hardcoding macosx is def not the right solution, so if you are targetting a different platform (check out https://github.com/martin31821/apple-libtapi/blob/2.0.0/src/llvm/projects/libtapi/include/tapi/Core/YAML.h#L62) try sedding that and see if it works. My expectations are at the lowest ever.

What did work for me: compiling the hello world test.cpp on linux, and running it on OSX.

martin31821 commented 5 years ago

Yep, that worked for me too. I used the Xcode 10.1 beta image and extracted the SDK from it. We're using it to compile cxx17 programs for OSX 10.14

steeve commented 5 years ago

For the record, on my macos machine, here's what's in libSystem.tbd:

--- !tapi-tbd-v3
archs:           [ x86_64 ]
uuids:           [ 'x86_64: XXXXXXXX' ] <- removed just in case
platform:        macosx
install-name:    /usr/lib/libSystem.B.dylib
current-version: 1252.200.5
objc-constraint: none

So I'm not sure why there is zippered in yours ?