MantisBT - CMake
View Issue Details
0015855CMakeCMakepublic2015-11-22 17:012016-05-02 08:30
Raphael Kubo da Costa 
Brad King 
normalmajoralways
closedfixed 
FreeBSD
CMake 3.4 
CMake 3.4.1CMake 3.4.1 
0015855: "Disable shared library support when compiler links statically" forbids shared library builds with -nostdlib
Commit 8b4873a1b025d7f47c90f3cafc93ca152b9e4652 ("Disable shared library support when compiler links statically") introduced a check in Modules/CMakeDetermineCompilerId.cmake that ends up running file(1) on the compiler itself and assumes it cannot generate shared libraries if file(1) says the compiler was statically linked.

This breaks builds if one passes "-nostdlib" in C{XX}FLAGS and there's an ADD_LIBRARY(... SHARED ...) target:

CMake Warning (dev) at CMakeLists.txt:3 (add_library):
  ADD_LIBRARY called with SHARED option but the target platform does not
  support dynamic linking. Building a STATIC library instead. This may lead
  to problems.

This happens because if -nostdlib is passed, as running the binary produced by CMakeC{XX}CompilerId.c{pp}.in in will be statically linked (and crash on startup, even though it is not relevant here).

On FreeBSD, this breaks the build of ports (packages) such as libcxxrt, which, being a very low-level library that provides features to a compiler, needs to be built with -nostdlib. This is not limited to FreeBSD, however, as the same file(1) returns a false-positive on Linux as well.
foo.c:
int foo(void) { return 42; }

CMakeLists.txt:
project(foo)
cmake_minimum_required(VERSION 3.4)
add_library(foo SHARED foo.c)

It will produce libfoo.a, not libfoo.so.
No tags attached.
Issue History
2015-11-22 17:01Raphael Kubo da CostaNew Issue
2015-11-23 09:47Brad KingAssigned To => Brad King
2015-11-23 09:47Brad KingStatusnew => assigned
2015-11-23 09:47Brad KingTarget Version => CMake 3.4.1
2015-11-23 10:18Brad KingNote Added: 0039901
2015-11-23 10:19Brad KingNote Added: 0039902
2015-11-23 10:19Brad KingStatusassigned => resolved
2015-11-23 10:19Brad KingResolutionopen => fixed
2015-11-23 10:19Brad KingFixed in Version => CMake 3.4.1
2016-05-02 08:30Robert MaynardNote Added: 0040966
2016-05-02 08:30Robert MaynardStatusresolved => closed

Notes
(0039901)
Brad King   
2015-11-23 10:18   
Thanks. We'll have to revert that commit and look for another solution to the problem it tried to address:

 Revert "Disable shared library support when compiler links statically"
 https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=9682de56 [^]
(0039902)
Brad King   
2015-11-23 10:19   
I've queued this change for merge to the 'release' branch for inclusion in 3.4.1.
(0040966)
Robert Maynard   
2016-05-02 08:30   
Closing resolved issues that have not been updated in more than 4 months.