android-tools icon indicating copy to clipboard operation
android-tools copied to clipboard

Darwin support

Open Atemu opened this issue 2 years ago • 10 comments

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 avatar Apr 20 '22 17:04 Atemu

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.

Atemu avatar Apr 20 '22 17:04 Atemu

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

anatol avatar Apr 20 '22 18:04 anatol

How would I do that?

Atemu avatar Apr 20 '22 23:04 Atemu

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

anatol avatar Apr 20 '22 23:04 anatol

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.

Atemu avatar Apr 22 '22 07:04 Atemu

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

asdfugil avatar May 23 '22 12:05 asdfugil

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.

Atemu avatar May 24 '22 09:05 Atemu

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

asdfugil avatar May 24 '22 09:05 asdfugil

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

Atemu avatar May 24 '22 09:05 Atemu

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.

asdfugil avatar May 24 '22 09:05 asdfugil

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

Would be great to get their patches upstream.

Atemu avatar Nov 04 '22 11:11 Atemu

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.

Biswa96 avatar Nov 10 '22 18:11 Biswa96

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

anatol avatar Nov 10 '22 19:11 anatol

Let me try.

Biswa96 avatar Nov 10 '22 19:11 Biswa96

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

anatol avatar Nov 12 '22 02:11 anatol

BTW, e2fsdroid is not enabled in macos yet. I tried to build it but libselinux fails in macos. If I examine the e2fsdriod mach-o binary it has some function from libselinux. For example, here disassembled code of freecon

$ objdump --disassemble-symbols=_freecon e2fsdroid~.x64

e2fsdroid~.x64: file format mach-o 64-bit x86-64

Disassembly of section __TEXT,__text:

0000000100040ee0 <_freecon>:
100040ee0: 55                           pushq   %rbp
100040ee1: 48 89 e5                     movq    %rsp, %rbp
100040ee4: 5d                           popq    %rbp
100040ee5: e9 1c 87 09 00               jmp     0x1000d9606 <dyld_stub_binder+0x1000d9606>

So, I think it can be enabled. I'll look into it later.

Biswa96 avatar Nov 12 '22 03:11 Biswa96