TritonDataCenter / pkgsrc

NetBSD/pkgsrc fork for our binary package repositories
https://pkgsrc.smartos.org/
131 stars 51 forks source link

macos replace xcode clang with pkgsrc #371

Closed smurfd closed 7 months ago

smurfd commented 8 months ago

Hey,

Really appreciate the work you do.

Ive been trying to get clang to work on macos on and off a ton of times. Is there anyone using the binary packages for clang on macos as main compiler? (meaning replacing the xcode's clang) What options have you set to get it to work!? Installed from : https://pkgsrc.joyent.com/install-on-macos/

$ uname -a
Darwin brr.local 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:54:51 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6030 arm64

Ofcource adding /opt/pkg/bin first in PATH after adding a symlink to ld -> lld in /opt/pkg/bin (otherwise it would try to use /usr/bin/ld)

#include <stdio.h>

int main() {
  return 0;
}

If i'd add a printf("hi\n"); i get this:

error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  printf("hi\n");

This is the errors i get (added macos sdk include to try to get rid of the printf error above): $ PATH=/opt/pkg/bin:$PATH LDFLAGS="-L/opt/pkg/lib -Wl,-rpath,/opt/pkg/lib" CPPFLAG="-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ -I/opt/pkg/include -I/opt/pkg/include/c++/v1/" /opt/pkg/bin/clang -Wimplicit-function-declaration -v c.c

clang version 16.0.6
Target: x86_64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /opt/pkg/bin
 "/opt/pkg/bin/clang-16" -cc1 -triple x86_64-apple-macosx14.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name c.c -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 1015.7 -v -fcoverage-compilation-dir=/Users/willygorilla/dev -resource-dir /opt/pkg/lib/clang/16 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/local/include -internal-isystem /opt/pkg/lib/clang/16/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include -Wimplicit-function-declaration -fdebug-compilation-dir=/Users/willygorilla/dev -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/qz/b881qqrj15s4ftjcv08kdngh0000gn/T/c-6b495e.o -x c c.c
clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target x86_64-apple-darwin23.2.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /opt/pkg/lib/clang/16/include
End of search list.
 "/opt/pkg/bin/ld" -demangle -lto_library /opt/pkg/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -platform_version macos 14.0.0 14.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -o a.out /var/folders/qz/b881qqrj15s4ftjcv08kdngh0000gn/T/c-6b495e.o -lSystem
ld: error: unknown argument '-no_deduplicate'
ld: error: unknown argument '-dynamic', did you mean '-Bdynamic'
ld: error: unknown argument '-arch'
ld: error: unknown argument '-platform_version'
ld: error: unknown argument '-syslibroot'
ld: error: unable to find library -lto_library
ld: error: /opt/pkg/lib/libLTO.dylib: unknown file type
ld: error: cannot open x86_64: No such file or directory
ld: error: cannot open macos: No such file or directory
ld: error: cannot open 14.0.0: No such file or directory
ld: error: cannot open 14.0.0: No such file or directory
ld: error: cannot open /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk: No such file or directory
ld: error: /var/folders/qz/b881qqrj15s4ftjcv08kdngh0000gn/T/c-6b495e.o: unknown file type
ld: error: unable to find library -lSystem
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)

I have seen this question asked around, could be good to add to a documentation Thanks in advance.

smurfd commented 7 months ago

Solved it, First i linked the current Xcode Command line tools SDK to 11.3 sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX14.2.sdk /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk Then added these two lined to my ~.zshrc

alias clang="/opt/pkg/bin/clang --target=aarch64-apple-darwin23.2.0 -fuse-ld=lld"
alias clang++="/opt/pkg/bin/clang++ --target=aarch64-apple-darwin23.2.0 -fuse-ld=lld"
$ clang -v c.c
clang version 16.0.6
Target: aarch64-apple-darwin23.2.0
Thread model: posix
InstalledDir: /opt/pkg/bin
 "/opt/pkg/bin/clang-16" -cc1 -triple arm64-apple-macosx14.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name c.c -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=lldb -target-linker-version 1015.7 -v -fcoverage-compilation-dir=/Users/willygorilla/dev -resource-dir /opt/pkg/lib/clang/16 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/local/include -internal-isystem /opt/pkg/lib/clang/16/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include -fdebug-compilation-dir=/Users/willygorilla/dev -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -fcolor-diagnostics -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/qz/b881qqrj15s4ftjcv08kdngh0000gn/T/c-7d327c.o -x c c.c
clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target x86_64-apple-darwin23.2.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /opt/pkg/lib/clang/16/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/opt/pkg/bin/ld64.lld" -demangle -no_deduplicate -dynamic -arch arm64 -platform_version macos 14.0.0 14.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -o a.out /var/folders/qz/b881qqrj15s4ftjcv08kdngh0000gn/T/c-7d327c.o -lSystem
$ file a.out 
a.out: Mach-O 64-bit executable arm64
$ ./a.out 
hi