Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't dynamically link to libuv on macOS #376

Merged
merged 1 commit into from
May 16, 2023
Merged

Conversation

nealrichardson
Copy link
Contributor

Fixes #374

The linker on macOS always prefers to dynamically link, so if you have both shared and static versions of a library, the shared will be picked. To force static linking yet still use system libuv if found, this PR copies libuv.a to a different directory and renames it to libuv_static.a, and that's what gets passed in the PKG_LIBS.

Before, you see it linking to the dylib:

% R CMD INSTALL .
* installing to library ‘~/Library/R/arm64/4.3/library’
* installing *source* package ‘httpuv’ ...
** using staged installation
** Using libuv found by pkg-config in /opt/homebrew/Cellar/libuv/1.44.2
** PKG_CFLAGS=-I/opt/homebrew/Cellar/libuv/1.44.2/include
** PKG_LIBS=-L/opt/homebrew/Cellar/libuv/1.44.2/lib -luv
** libs...
* DONE (httpuv)

% otool -L ~/Library/R/arm64/4.3/library/httpuv/libs/httpuv.so | grep libuv
	/opt/homebrew/opt/libuv/lib/libuv.1.dylib (compatibility version 2.0.0, current version 2.0.0)

After, there is no link to the dylib:

% R CMD INSTALL .        
* installing to library ‘~/Library/R/arm64/4.3/library’
* installing *source* package ‘httpuv’ ...
** using staged installation
** Using static libuv found by pkg-config in /opt/homebrew/Cellar/libuv/1.44.2
** PKG_CFLAGS=-I/opt/homebrew/Cellar/libuv/1.44.2/include
** PKG_LIBS=-L/Users/npr/code/httpuv/src/libuv/.libs -luv_static
...

% otool -L ~/Library/R/arm64/4.3/library/httpuv/libs/httpuv.so | grep libuv

%

To confirm the fallback behavior, I deleted libuv.a, so pkg-config will still find libuv, but the static library isn't there. It correctly switches to the bundled build:

cp: /opt/homebrew/Cellar/libuv/1.44.2/lib/libuv.a: No such file or directory
** pkg-config did not find static libuv
** Using bundled copy of libuv
** PKG_CFLAGS=-Ilibuv/include
** PKG_LIBS=./libuv/.libs/libuv.a

@nealrichardson nealrichardson requested a review from jcheng5 May 16, 2023 15:10
@jcheng5 jcheng5 merged commit 4b8dc26 into main May 16, 2023
@jcheng5 jcheng5 deleted the force-static-macos branch May 16, 2023 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't dynamically link to libuv on macOS
2 participants