···11+# Platform Notes {#chap-platform-notes}
22+33+## Darwin (macOS) {#sec-darwin}
44+55+Some common issues when packaging software for Darwin:
66+77+- The Darwin `stdenv` uses clang instead of gcc. When referring to the compiler `$CC` or `cc` will work in both cases. Some builds hardcode gcc/g++ in their build scripts, that can usually be fixed with using something like `makeFlags = [ "CC=cc" ];` or by patching the build scripts.
88+99+ ```nix
1010+ stdenv.mkDerivation {
1111+ name = "libfoo-1.2.3";
1212+ # ...
1313+ buildPhase = ''
1414+ $CC -o hello hello.c
1515+ '';
1616+ }
1717+ ```
1818+1919+- On Darwin, libraries are linked using absolute paths, libraries are resolved by their `install_name` at link time. Sometimes packages won’t set this correctly causing the library lookups to fail at runtime. This can be fixed by adding extra linker flags or by running `install_name_tool -id` during the `fixupPhase`.
2020+2121+ ```nix
2222+ stdenv.mkDerivation {
2323+ name = "libfoo-1.2.3";
2424+ # ...
2525+ makeFlags = lib.optional stdenv.isDarwin "LDFLAGS=-Wl,-install_name,$(out)/lib/libfoo.dylib";
2626+ }
2727+ ```
2828+2929+- Even if the libraries are linked using absolute paths and resolved via their `install_name` correctly, tests can sometimes fail to run binaries. This happens because the `checkPhase` runs before the libraries are installed.
3030+3131+ This can usually be solved by running the tests after the `installPhase` or alternatively by using `DYLD_LIBRARY_PATH`. More information about this variable can be found in the *dyld(1)* manpage.
3232+3333+ ```
3434+ dyld: Library not loaded: /nix/store/7hnmbscpayxzxrixrgxvvlifzlxdsdir-jq-1.5-lib/lib/libjq.1.dylib
3535+ Referenced from: /private/tmp/nix-build-jq-1.5.drv-0/jq-1.5/tests/../jq
3636+ Reason: image not found
3737+ ./tests/jqtest: line 5: 75779 Abort trap: 6
3838+ ```
3939+4040+ ```nix
4141+ stdenv.mkDerivation {
4242+ name = "libfoo-1.2.3";
4343+ # ...
4444+ doInstallCheck = true;
4545+ installCheckTarget = "check";
4646+ }
4747+ ```
4848+4949+- Some packages assume xcode is available and use `xcrun` to resolve build tools like `clang`, etc. This causes errors like `xcode-select: error: no developer tools were found at '/Applications/Xcode.app'` while the build doesn’t actually depend on xcode.
5050+5151+ ```nix
5252+ stdenv.mkDerivation {
5353+ name = "libfoo-1.2.3";
5454+ # ...
5555+ prePatch = ''
5656+ substituteInPlace Makefile \
5757+ --replace '/usr/bin/xcrun clang' clang
5858+ '';
5959+ }
6060+ ```
6161+6262+ The package `xcbuild` can be used to build projects that really depend on Xcode. However, this replacement is not 100% compatible with Xcode and can occasionally cause issues.
-83
doc/stdenv/platform-notes.xml
···11-<chapter xmlns="http://docbook.org/ns/docbook"
22- xmlns:xlink="http://www.w3.org/1999/xlink"
33- xml:id="chap-platform-notes">
44- <title>Platform Notes</title>
55- <section xml:id="sec-darwin">
66- <title>Darwin (macOS)</title>
77-88- <para>
99- Some common issues when packaging software for Darwin:
1010- </para>
1111-1212- <itemizedlist>
1313- <listitem>
1414- <para>
1515- The Darwin <literal>stdenv</literal> uses clang instead of gcc. When referring to the compiler <varname>$CC</varname> or <command>cc</command> will work in both cases. Some builds hardcode gcc/g++ in their build scripts, that can usually be fixed with using something like <literal>makeFlags = [ "CC=cc" ];</literal> or by patching the build scripts.
1616- </para>
1717-<programlisting>
1818-stdenv.mkDerivation {
1919- name = "libfoo-1.2.3";
2020- # ...
2121- buildPhase = ''
2222- $CC -o hello hello.c
2323- '';
2424-}
2525-</programlisting>
2626- </listitem>
2727- <listitem>
2828- <para>
2929- On Darwin, libraries are linked using absolute paths, libraries are resolved by their <literal>install_name</literal> at link time. Sometimes packages won't set this correctly causing the library lookups to fail at runtime. This can be fixed by adding extra linker flags or by running <command>install_name_tool -id</command> during the <function>fixupPhase</function>.
3030- </para>
3131-<programlisting>
3232-stdenv.mkDerivation {
3333- name = "libfoo-1.2.3";
3434- # ...
3535- makeFlags = lib.optional stdenv.isDarwin "LDFLAGS=-Wl,-install_name,$(out)/lib/libfoo.dylib";
3636-}
3737-</programlisting>
3838- </listitem>
3939- <listitem>
4040- <para>
4141- Even if the libraries are linked using absolute paths and resolved via their <literal>install_name</literal> correctly, tests can sometimes fail to run binaries. This happens because the <varname>checkPhase</varname> runs before the libraries are installed.
4242- </para>
4343- <para>
4444- This can usually be solved by running the tests after the <varname>installPhase</varname> or alternatively by using <varname>DYLD_LIBRARY_PATH</varname>. More information about this variable can be found in the <citerefentry>
4545- <refentrytitle>dyld</refentrytitle>
4646- <manvolnum>1</manvolnum></citerefentry> manpage.
4747- </para>
4848-<programlisting>
4949-dyld: Library not loaded: /nix/store/7hnmbscpayxzxrixrgxvvlifzlxdsdir-jq-1.5-lib/lib/libjq.1.dylib
5050-Referenced from: /private/tmp/nix-build-jq-1.5.drv-0/jq-1.5/tests/../jq
5151-Reason: image not found
5252-./tests/jqtest: line 5: 75779 Abort trap: 6
5353-</programlisting>
5454-<programlisting>
5555-stdenv.mkDerivation {
5656- name = "libfoo-1.2.3";
5757- # ...
5858- doInstallCheck = true;
5959- installCheckTarget = "check";
6060-}
6161-</programlisting>
6262- </listitem>
6363- <listitem>
6464- <para>
6565- Some packages assume xcode is available and use <command>xcrun</command> to resolve build tools like <command>clang</command>, etc. This causes errors like <code>xcode-select: error: no developer tools were found at '/Applications/Xcode.app'</code> while the build doesn't actually depend on xcode.
6666- </para>
6767-<programlisting>
6868-stdenv.mkDerivation {
6969- name = "libfoo-1.2.3";
7070- # ...
7171- prePatch = ''
7272- substituteInPlace Makefile \
7373- --replace '/usr/bin/xcrun clang' clang
7474- '';
7575-}
7676-</programlisting>
7777- <para>
7878- The package <literal>xcbuild</literal> can be used to build projects that really depend on Xcode. However, this replacement is not 100% compatible with Xcode and can occasionally cause issues.
7979- </para>
8080- </listitem>
8181- </itemizedlist>
8282- </section>
8383-</chapter>