Skip to content

Add static as keyword to find_library #3747

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

Merged
merged 11 commits into from
Mar 11, 2019

Conversation

NickeZ
Copy link
Contributor

@NickeZ NickeZ commented Jun 17, 2018

This patch allows the build file author to enforce static or dynamic linkage. I've found this to be necessary when the two versions are not equivalent.

AFAIK there are not really any naming standards when it comes to libraries on Windows. But from my experience it is more common to prefix the static version with 'lib' and not have any prefixes for the dynamic library. This patch therefore changes the order of the prefixes so that dynamic libraries are found first which seem to be the case already on other platforms.

@tp-m
Copy link
Member

tp-m commented Jun 17, 2018

Would it make sense to have different keywords like prefer_static and require_static instead of just static?

@NickeZ
Copy link
Contributor Author

NickeZ commented Jun 17, 2018

Yeah I guess there are four combinations

  • require static (fail if static not found)
  • require shared (fail if shared not found)
  • prefer static (shared is fallback) <- This is currently default on windows.
  • prefer shared (static is fallback) <- This is currently default on non-windows

I don't think "require anything" can be implemented on windows because there are no ways to determine if the found library is static or shared. EDIT: I've fixed this in the next commit. We can use lib /list to see if a file is an "import library" and not a "static library".

An additional thing. I noted that meson itself produces lib<name>.a for static libraries on windows and <name>.dll for shared libraries. Which is an additional reason to have the right priority on the prefixes.

@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 65e00eb to 6e4ed2f Compare June 17, 2018 15:42
@NickeZ NickeZ force-pushed the nickez/select-dynamic branch 3 times, most recently from 82aa05a to be47d51 Compare June 17, 2018 21:31
if shlibname in stdout:
return True
return False
raise AssertionError('BUG: unkonwn libtype: {!r}'.format(libtype))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unkonwn -> unknown

if libtype == 'static':
if shlibname in stdout:
return False
return True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just write return (shlibname not in stdout) here, and likewise the negation of that below...

# FIXME: shared-static and static-shared return the same for now
if libtype in ('shared-static', 'static-shared'):
return True
proc = subprocess.Popen(['lib', '/list', filename], stdout=subprocess.PIPE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how safe the assumption that lib is in the path is. Perhaps the potential exception raised if it can't be found should be handled.

I guess there is no possibility of a cross-tool being required.

@nirbheek
Copy link
Member

This conflicted directly with #3691, so it needs a rebase. I will review it after that.

@nirbheek nirbheek self-assigned this Jun 18, 2018
@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 04bec59 to 23ab3bd Compare June 18, 2018 21:51
stdout = stdout.decode('utf-8')
shlibname = filename.with_suffix('.dll').name
if libtype == 'static':
return not shlibname in stdout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Flake8]

[E713] test for membership should be 'not in'

@NickeZ NickeZ force-pushed the nickez/select-dynamic branch 3 times, most recently from 45934b5 to c081c20 Compare June 18, 2018 22:09
@NickeZ
Copy link
Contributor Author

NickeZ commented Jun 18, 2018

Thanks for the comments. I've updated and rebased. Ready for review @nirbheek

@nirbheek
Copy link
Member

Test failure seems to be relevant.

@NickeZ NickeZ force-pushed the nickez/select-dynamic branch 2 times, most recently from 5891c3a to 86c4ad3 Compare June 19, 2018 14:31
@NickeZ
Copy link
Contributor Author

NickeZ commented Jun 20, 2018

@nirbheek Fixed test failure

@NickeZ
Copy link
Contributor Author

NickeZ commented Jun 28, 2018

ping!

@nacho
Copy link
Contributor

nacho commented Sep 6, 2018

I guess this will need another rebase

@nirbheek nirbheek added this to the 0.48.0 milestone Sep 6, 2018
@NickeZ
Copy link
Contributor Author

NickeZ commented Sep 6, 2018

Before rebasing I would like to know if the PR is desired or not.

@nirbheek nirbheek modified the milestones: 0.48.0, 0.49.0 Sep 20, 2018
@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 86c4ad3 to 599d886 Compare October 10, 2018 21:37
@NickeZ
Copy link
Contributor Author

NickeZ commented Oct 10, 2018

rebased

@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 599d886 to a1ee6b1 Compare October 10, 2018 22:36
@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 57b0fb9 to b56579f Compare February 26, 2019 18:27
@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 26, 2019

OK, i've rebased and changed the kwarg to static. There are changes in this PR that are not needed to actually merge the static-kwarg feature. Do you want me to move any such changes to another PR?

@nirbheek
Copy link
Member

That's always a good idea for minimizing delays in getting a PR merged. Small PRs are easy to review and merge :)

@NickeZ NickeZ force-pushed the nickez/select-dynamic branch from 0a6db0b to 2d8d656 Compare February 26, 2019 20:08
@jpakkane
Copy link
Member

Have not looked at the implementation but featurewise this is ok.

@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 26, 2019

Tests passes on my machine now. Waiting for review.

@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 26, 2019

One failure seems related. Is it on purpose that it links statically to libm? That seems wrong to me. I'm not sure what goes wrong here actually..


======================================================================
ERROR: test_pkgconfig_static (__main__.AllPlatformTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "run_unittests.py", line 144, in wrapped
    return f(*args, **kwargs)
  File "run_unittests.py", line 2401, in test_pkgconfig_static
    self.build()
  File "run_unittests.py", line 1208, in build
    return self._run(self.build_command + args + extra_args, workdir=self.builddir)
  File "run_unittests.py", line 1156, in _run
    raise subprocess.CalledProcessError(p.returncode, command, output=p.stdout)
subprocess.CalledProcessError: Command '['ninja', '-w', 'dupbuild=err', '-d', 'explain']' returned non-zero exit status 1.

Stdout:
The Meson build system
Version: 0.49.999
Source dir: D:\a\1\s\test cases\unit\18 pkgconfig static
Build dir: C:\Users\VSSADM~1\AppData\Local\Temp\tmpi_umei5o
Build type: native build
Project name: pkg-config static
Project version: undefined
Native C compiler: clang (clang 7.0.1 "clang version 7.0.1 (tags/RELEASE_701/final)")
Build machine cpu family: x86_64
Build machine cpu: x86_64
Message: /D/a/1/s/test cases/unit/18 pkgconfig static
Configuring foo.pc using configuration
Found pkg-config: D:\a\msys64\mingw64\bin\pkg-config.EXE (0.29.2)
Dependency foo found: YES 1.0
Build targets in project: 1
Found ninja-1.9.0 at 'D:\a\msys64\mingw64\bin\ninja.EXE'

ninja explain: deps for 'foomain@exe/main.c.obj' are missing
ninja explain: foomain@exe/main.c.obj is dirty
ninja explain: /a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a has no in-edge and is missing
ninja explain: /a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a is dirty
ninja explain: foomain.exe is dirty
ninja: error: '/a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a', needed by 'foomain.exe', missing and no known rule to make it


----------------------------------------------------------------------
Ran 216 tests in 164.934s

FAILED (errors=1, skipped=75)

Stdout:
The Meson build system
Version: 0.49.999
Source dir: D:\a\1\s\test cases\unit\18 pkgconfig static
Build dir: C:\Users\VSSADM~1\AppData\Local\Temp\tmpi_umei5o
Build type: native build
Project name: pkg-config static
Project version: undefined
Native C compiler: clang (clang 7.0.1 "clang version 7.0.1 (tags/RELEASE_701/final)")
Build machine cpu family: x86_64
Build machine cpu: x86_64
Message: /D/a/1/s/test cases/unit/18 pkgconfig static
Configuring foo.pc using configuration
Found pkg-config: D:\a\msys64\mingw64\bin\pkg-config.EXE (0.29.2)
Dependency foo found: YES 1.0
Build targets in project: 1
Found ninja-1.9.0 at 'D:\a\msys64\mingw64\bin\ninja.EXE'

ninja explain: deps for 'foomain@exe/main.c.obj' are missing
ninja explain: foomain@exe/main.c.obj is dirty
ninja explain: /a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a has no in-edge and is missing
ninja explain: /a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a is dirty
ninja explain: foomain.exe is dirty
ninja: error: '/a/msys64/mingw64/x86_64-w64-mingw32/lib/libm.a', needed by 'foomain.exe', missing and no known rule to make it

@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 26, 2019

I can't reproduce the error locally. I installed msys2 and mingw-x86_64 and that test passes on my machine.

@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 27, 2019

I spoke to soon, it fails, but I get another error:

test_pkgconfig_static (__main__.AllPlatformTests) ... ERROR

Stdout:
The Meson build system
Version: 0.49.999
Source dir: C:/msys64/home/nicke/git/meson/test cases/unit/18 pkgconfig static
Build dir: C:/Users/nicke/AppData/Local/Temp/tmp5b1uajae
Build type: native build
Project name: pkg-config static
Project version: undefined
Native C compiler: clang (clang 7.0.1 "clang version 7.0.1 (tags/RELEASE_701/final)")
Build machine cpu family: x86_64
Build machine cpu: x86_64
Message: /C/
Configuring foo.pc using configuration
Found pkg-config: C:\msys64\mingw64\bin/pkg-config.EXE (0.29.2)
WARNING: Static library 'foo' not found for dependency 'foo', may not be statically linked
Dependency foo found: YES 1.0
Build targets in project: 1
Found ninja-1.9.0 at 'C:\msys64\mingw64\bin/ninja.EXE'

[1/2] Compiling C object foomain@exe/main.c.obj.
FAILED: foomain@exe/main.c.obj
clang @foomain@exe/main.c.obj.rsp
../../../../../../msys64/home/nicke/git/meson/test cases/unit/18 pkgconfig static/main.c:1:10: fatal error: 'foo.h' file not found
#include <foo.h>
         ^~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
ninja explain: deps for 'foomain@exe/main.c.obj' are missing
ninja explain: foomain@exe/main.c.obj is dirty
ninja explain: foomain.exe is dirty


@NickeZ
Copy link
Contributor Author

NickeZ commented Feb 27, 2019

yay, I fixed the bug on CI. The issue I had on my machine was unrelated.

@TheQwertiest
Copy link
Contributor

0.50?

Copy link
Member

@dcbaker dcbaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think this is a nice change, there is a little bit of user facing language changes that @jpakkane should look at. We should probably also extend dependency() to understand the "either but prefer X" syntax at some point as well.

We're past feature freeze for 0.50, and only a couple of days away from release so I think this will need to wait for 0.51. Please also write up a documentation snippet and the make the doc changes to the manual and reference manual about this new keyword. Though let's not do that until we get done bikeshedding the name.

@dcbaker
Copy link
Member

dcbaker commented Mar 6, 2019

I blamed it, the static-shared/shared-static madness is from late 2017, so I guess I can't really ask to change it now, but those are terrible names, especially for dyslexics :(

@NickeZ
Copy link
Contributor Author

NickeZ commented Mar 6, 2019

I blamed it, the static-shared/shared-static madness is from late 2017, so I guess I can't really ask to change it now, but those are terrible names, especially for dyslexics :(

It is (still) an implementation detail, so I can change it if there is consensus on a better name. There is quite a high probability that something like this will sneak out into the public API and changing it now might nudge the potentially public API into a better direction.

@NickeZ
Copy link
Contributor Author

NickeZ commented Mar 6, 2019

Added some docs

@dcbaker
Copy link
Member

dcbaker commented Mar 7, 2019

I'll send a separate PR for the rename.

Test failures look unrelated BTW.

@dcbaker dcbaker added this to the 0.51.0 milestone Mar 7, 2019
@jpakkane jpakkane merged commit dd2c44c into mesonbuild:master Mar 11, 2019
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.

10 participants