nmeum / android-tools

Unoffical CMake-based build system for android command line utilities
Apache License 2.0
191 stars 57 forks source link

Darwin support #55

Closed Atemu closed 1 year ago

Atemu commented 2 years ago

Trying to build on Darwin/macOS fails with this error:

...
[ 17%] Building CXX object vendor/CMakeFiles/libadb.dir/adb/services.cpp.o
[ 17%] Built target libandroidfw
[ 17%] Building CXX object vendor/CMakeFiles/libadb.dir/adb/socket_spec.cpp.o
[ 18%] Building CXX object vendor/CMakeFiles/libadb.dir/adb/sysdeps_unix.cpp.o
/tmp/nix-build-android-tools-31.0.3p1.drv-0/android-tools-31.0.3p1/vendor/adb/client/usb_linux.cpp:27:10: fatal error: 'linux/usb/ch9.h' file not found
#include <linux/usb/ch9.h>
         ^~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [vendor/CMakeFiles/libadb.dir/build.make:318: vendor/CMakeFiles/libadb.dir/adb/client/usb_linux.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 18%] Building CXX object vendor/CMakeFiles/libadb.dir/adb/sysdeps/errno.cpp.o
make[1]: *** [CMakeFiles/Makefile2:438: vendor/CMakeFiles/libadb.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

This sounds wrong to me as I shouldn't have to care about Linux-specific headers.

Atemu commented 2 years ago

https://github.com/nmeum/android-tools/blob/68de70185d81809a36efba6841da46a041f929a2/vendor/CMakeLists.adb.txt#L27

Using usb_osx.cpp passes this error and now fails with

Scanning dependencies of target fipsmodule
[ 16%] Building C object vendor/CMakeFiles/libselinux.dir/selinux/libselinux/src/seusers.c.o
[ 16%] Building C object vendor/CMakeFiles/libselinux.dir/selinux/libselinux/src/selinux_config.c.o
/tmp/nix-build-android-tools-31.0.3p1.drv-0/android-tools-31.0.3p1/vendor/selinux/libselinux/src/init.c:8:10: fatal error: 'stdio_ext.h' file not found
#include <stdio_ext.h>
         ^~~~~~~~~~~~~
1 error generated.
make[2]: *** [vendor/CMakeFiles/libselinux.dir/build.make:179: vendor/CMakeFiles/libselinux.dir/selinux/libselinux/src/init.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....

Many

error: too many arguments to function call, expected 4, have 5

and a few more errors stemming from libselinux.

I have a feeling this lib shouldn't be used at all on Darwin.

anatol commented 2 years ago

Indeed, libselinux is a Linux-specific library. Could you please temporarily disable it?

Atemu commented 2 years ago

How would I do that?

anatol commented 2 years ago

I would start with commenting out libselinux and libsepool related parts at

https://github.com/nmeum/android-tools/blob/8d839f86359b90ed61942e04a0041a61b9792bc8/vendor/CMakeLists.fastboot.txt#L50

Atemu commented 2 years ago

That does it, thanks.

Now getting stuck on

/tmp/android-tools-31.0.3p1/vendor/logging/liblog/logger_write.cpp:141:53: error: use of undeclared identifier 'getprogname'
  static std::string* default_tag = new std::string(getprogname());
                                                    ^
1 error generated.
make[2]: *** [vendor/CMakeFiles/liblog.dir/build.make:132: vendor/CMakeFiles/liblog.dir/logging/liblog/logger_write.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/tmp/android-tools-31.0.3p1/vendor/logging/liblog/logprint.cpp:466:27: error: use of undeclared identifier 'strsep'
  while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
                          ^
[ 86%] Built target ssl
1 error generated.
make[2]: *** [vendor/CMakeFiles/liblog.dir/build.make:160: vendor/CMakeFiles/liblog.dir/logging/liblog/logprint.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:514: vendor/CMakeFiles/liblog.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

That seems unusual as those should be very basic functions.

asdfugil commented 2 years ago

That does it, thanks.

Now getting stuck on

/tmp/android-tools-31.0.3p1/vendor/logging/liblog/logger_write.cpp:141:53: error: use of undeclared identifier 'getprogname'
  static std::string* default_tag = new std::string(getprogname());
                                                    ^
1 error generated.
make[2]: *** [vendor/CMakeFiles/liblog.dir/build.make:132: vendor/CMakeFiles/liblog.dir/logging/liblog/logger_write.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/tmp/android-tools-31.0.3p1/vendor/logging/liblog/logprint.cpp:466:27: error: use of undeclared identifier 'strsep'
  while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
                          ^
[ 86%] Built target ssl
1 error generated.
make[2]: *** [vendor/CMakeFiles/liblog.dir/build.make:160: vendor/CMakeFiles/liblog.dir/logging/liblog/logprint.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:514: vendor/CMakeFiles/liblog.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

That seems unusual as those should be very basic functions.

To fix this, make sure you have -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL -std=gnu++20 in CMAKE_CXX_FLAGS

Atemu commented 2 years ago
SET(CMAKE_CXX_FLAGS "-D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL -std=gnu++20")

fixes this problem, thanks!

Everything compiles now but it errors out on linking:

[230/230] Linking CXX executable vendor/adb
FAILED: vendor/adb 
: && /nix/store/mwrgq0sfrzdx0cndync3nkij8hh5wnmg-Toolchains/XcodeDefault.xctoolchain/bin/clang++ -std=gnu++2a -Wno-attributes -D_FILE_OFFSET_BITS=64 -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL -std=gnu++20 -g -arch arm64 -isysroot /nix/store/2hwiibhwvf2qb8rsvnwrzjnwvyqh0is2-xcodebuild-0.1.2-pre/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -mmacosx-version-min=11.0 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  vendor/CMakeFiles/adb.dir/adb/adb.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_io.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_listeners.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_trace.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_unique_fd.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_utils.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent_poll.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent_epoll.cpp.o vendor/CMakeFiles/adb.dir/adb/shell_service_protocol.cpp.o vendor/CMakeFiles/adb.dir/adb/sockets.cpp.o vendor/CMakeFiles/adb.dir/adb/socket_spec.cpp.o vendor/CMakeFiles/adb.dir/adb/transport.cpp.o vendor/CMakeFiles/adb.dir/adb/transport_fd.cpp.o vendor/CMakeFiles/adb.dir/adb/client/transport_local.cpp.o vendor/CMakeFiles/adb.dir/adb/client/transport_usb.cpp.o vendor/CMakeFiles/adb.dir/adb/types.cpp.o -o vendor/adb  vendor/liblibadb.a  vendor/liblibadb_crypto_defaults.a  vendor/liblibadb_tls_connection_defaults.a  vendor/liblibbase.a  vendor/liblibcrypto.a  vendor/liblibadb_sysdeps.a  vendor/liblibcutils.a  vendor/liblibdiagnoseusb.a  vendor/libliblog.a  vendor/liblibzip.a  vendor/boringssl/crypto/libcrypto.a  vendor/boringssl/ssl/libssl.a  /nix/store/0mmc1pjmfdpdqh94zv342w8i6hyc9hy1-protobuf-3.19.3/lib/libprotobuf.dylib  -lbrotlicommon  -lbrotlidec  -lbrotlienc  -llz4  -lpthread  -lusb-1.0  -lz  -lzstd  /nix/store/5639r075bisi2b7msdwn46djww7nprc1-fmt-7.1.3/lib/libfmt.7.1.3.dylib  vendor/boringssl/crypto/libcrypto.a  -lpthread && :
Undefined symbols for architecture arm64:
  "_CFUUIDGetConstantUUIDWithBytes", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_CFUUIDGetUUIDBytes", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOCreatePlugInInterfaceForService", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOIteratorNext", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOObjectRelease", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOServiceGetMatchingServices", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOServiceMatching", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_kCFAllocatorSystemDefault", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_kIOMasterPortDefault", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
ld: symbol(s) not found for architecture arm64
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

usb_osx.cpp uses IOKit which is in its include path and the IDE finds the connection.

asdfugil commented 2 years ago
SET(CMAKE_CXX_FLAGS "-D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL -std=gnu++20")

fixes this problem, thanks!

Everything compiles now but it errors out on linking:

[230/230] Linking CXX executable vendor/adb
FAILED: vendor/adb 
: && /nix/store/mwrgq0sfrzdx0cndync3nkij8hh5wnmg-Toolchains/XcodeDefault.xctoolchain/bin/clang++ -std=gnu++2a -Wno-attributes -D_FILE_OFFSET_BITS=64 -D_DARWIN_C_SOURCE -D__DARWIN_C_LEVEL=__DARWIN_C_FULL -std=gnu++20 -g -arch arm64 -isysroot /nix/store/2hwiibhwvf2qb8rsvnwrzjnwvyqh0is2-xcodebuild-0.1.2-pre/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -mmacosx-version-min=11.0 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  vendor/CMakeFiles/adb.dir/adb/adb.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_io.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_listeners.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_trace.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_unique_fd.cpp.o vendor/CMakeFiles/adb.dir/adb/adb_utils.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent_poll.cpp.o vendor/CMakeFiles/adb.dir/adb/fdevent/fdevent_epoll.cpp.o vendor/CMakeFiles/adb.dir/adb/shell_service_protocol.cpp.o vendor/CMakeFiles/adb.dir/adb/sockets.cpp.o vendor/CMakeFiles/adb.dir/adb/socket_spec.cpp.o vendor/CMakeFiles/adb.dir/adb/transport.cpp.o vendor/CMakeFiles/adb.dir/adb/transport_fd.cpp.o vendor/CMakeFiles/adb.dir/adb/client/transport_local.cpp.o vendor/CMakeFiles/adb.dir/adb/client/transport_usb.cpp.o vendor/CMakeFiles/adb.dir/adb/types.cpp.o -o vendor/adb  vendor/liblibadb.a  vendor/liblibadb_crypto_defaults.a  vendor/liblibadb_tls_connection_defaults.a  vendor/liblibbase.a  vendor/liblibcrypto.a  vendor/liblibadb_sysdeps.a  vendor/liblibcutils.a  vendor/liblibdiagnoseusb.a  vendor/libliblog.a  vendor/liblibzip.a  vendor/boringssl/crypto/libcrypto.a  vendor/boringssl/ssl/libssl.a  /nix/store/0mmc1pjmfdpdqh94zv342w8i6hyc9hy1-protobuf-3.19.3/lib/libprotobuf.dylib  -lbrotlicommon  -lbrotlidec  -lbrotlienc  -llz4  -lpthread  -lusb-1.0  -lz  -lzstd  /nix/store/5639r075bisi2b7msdwn46djww7nprc1-fmt-7.1.3/lib/libfmt.7.1.3.dylib  vendor/boringssl/crypto/libcrypto.a  -lpthread && :
Undefined symbols for architecture arm64:
  "_CFUUIDGetConstantUUIDWithBytes", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_CFUUIDGetUUIDBytes", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOCreatePlugInInterfaceForService", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOIteratorNext", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOObjectRelease", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOServiceGetMatchingServices", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_IOServiceMatching", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_kCFAllocatorSystemDefault", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
  "_kIOMasterPortDefault", referenced from:
      RunLoopThread() in liblibadb.a(usb_osx.cpp.o)
ld: symbol(s) not found for architecture arm64
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

usb_osx.cpp uses IOKit which is in its include path and the IDE finds the connection.

To fix this problem, set CMAKE_EXE_LINKER_FLAGS to -framework CoreFoundation -framework IOKit

Atemu commented 2 years ago

OMG, it works! Thank you so much! Do you already have some patches or why do you know all of this?

asdfugil commented 2 years ago

OMG, it works! Thank you so much! Do you already have some patches or why do you know all of this?

By inspecting stdint.h and string.h, it is found that some defines are guarded by #if defined(_DARWIN_C_SOURCE) or #if __DARWIN_C_LEVEL >= __DARWIN_C_FULL. So add CXX flags to satisfy them.

As for the undefined symbols part, according to the symbol names, they appear come from CoreFoundation and IOKit.

I am also compiling for Darwin, but I am targeting both iOS (which has extra problems) and macOS.

Atemu commented 1 year ago

@wegank fixed this in our builds for Nixpkgs in the PR above me.

Would be great to get their patches upstream.

Biswa96 commented 1 year ago

Just for fun, I have compiled android-tools in macOS in GitHub Actions using the nixpkgs changes. But I do not have any Apple hardware (nor will ever have 💰). If you can test the binaries I shall provide the CI artifacts from GitHub Actions run.

anatol commented 1 year ago

@Biswa96 If you provide a PR for MacOSX support I'll be glad to do some testing

Biswa96 commented 1 year ago

Let me try.

anatol commented 1 year ago

Fixed by https://github.com/nmeum/android-tools/commit/a1ab35b31525966e0f0770047cd82accb36d025b