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

Building static #8

Closed
sonoro1234 opened this issue Dec 28, 2021 · 8 comments
Closed

Building static #8

sonoro1234 opened this issue Dec 28, 2021 · 8 comments

Comments

@sonoro1234
Copy link

sonoro1234 commented Dec 28, 2021

Trying to build a static lib in windows (but I think this is not windows specific) fails.
PortMidi is used as a submodule to my main project Lua2SC, where I want some librarys to be SHARED and others to be STATIC.

There is also a warning about using BUILD_SHARED_LIBS in portmidi

CMake Warning (dev) at portmidi/CMakeLists.txt:21 (option):
  Policy CMP0077 is not set: option() honors normal variables.  Run "cmake
  --help-policy CMP0077" for policy details.  Use the cmake_policy command to
  set the policy and suppress this warning.

  For compatibility with older versions of CMake, option is clearing the
  normal variable 'BUILD_SHARED_LIBS'.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- PortMidi Library name: portmidi
-- Build type: Release
-- Library Type:
-- Compiler flags:
-- Compile definitions:
-- Compile options:
-- Compiler cxx debug flags: -g
-- Compiler cxx release flags: -O3 -DNDEBUG
-- Compiler cxx min size flags: -Os -DNDEBUG
-- Compiler cxx flags:

Solution would be to have a PortMidi specific variable ( as PM_LIB_SHARED for example) set by the user and then in pm_common/CMakeLists:

if(PM_LIB_SHARED)
     add_library(portmidi SHARED ${PM_LIB_PUBLIC_SRC})
else(PM_LIB_SHARED)
     add_library(portmidi STATIC ${PM_LIB_PUBLIC_SRC})
end(PM_LIB_SHARED)

Also, all other uses of BUILD_SHARED_LIBS should be changed to PM_LIB_SHARED

@sonoro1234
Copy link
Author

Update:

I have been able to compile static with this lines for adding the PortMidi subproject.
It works, but I wonder if it would be cleaner with PM_LIB_SHARED instead

#this is luaportmidi cmake
set(PORTMIDI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../portmidi)
set(OLD_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
add_subdirectory(${PORTMIDI_DIR} ${CMAKE_BINARY_DIR}/portmidi EXCLUDE_FROM_ALL)
set(BUILD_SHARED_LIBS ${OLD_BUILD_SHARED_LIBS})

@cryptomilk
Copy link

cryptomilk commented Dec 28, 2021

Normally all you have to do is to use:

add_library(portmidi ${PM_LIB_PUBLIC_SRC})

and then you can control wether you want a shared or a static lib with the BUILD_SHARED_LIBS variable!

@sonoro1234
Copy link
Author

Yes, the problem appears when you need more than just this library

@Be-ing
Copy link
Contributor

Be-ing commented Dec 28, 2021

Solution would be to have a PortMidi specific variable

No, that breaks CMake conventions as recommended in the CMake documentation. If you want to set a variable just for one add_subdirectory then I think #8 (comment) is an appropriate solution. Alternatively you can use ExternalProject_Add with CMAKE_ARGS -D BUILD_SHARED_LIBS=OFF or a package manager.

@sonoro1234
Copy link
Author

Yes, #8 (comment) solves it for me, but I dont think that https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html is telling that specific variables are breaking any CMake convention at all.

So, I dont mind specially what is done, but I dont think that this "CMake conventions" are more than suppositions.

@rbdannenberg
Copy link
Contributor

It seems like CMake has problems either way: both solutions create a "shadow variable" (either PM_BUILD_SHARED_LIBS or OLD_BUILD_SHARED_LIBS) to hide info from CMake where scope rules or modularity fail to do the job. Since directly using BUILD_SHARED_LIBS is already in place in PortMidi, I'm inclined to leave it -- at least it's simpler and easier to understand for most cases. I'm still open to counter-proposals/arguments. Maybe it's just my perception, but it seems CMake is constantly evolving, and perhaps this case will be better supported in some future release.

@Be-ing
Copy link
Contributor

Be-ing commented Dec 28, 2021

I dont think that this "CMake conventions" are more than suppositions

They are. Tooling is built around them. If you break them, you force packagers and downstream users to learn idiosyncratic hacks for one particular library.

@sonoro1234
Copy link
Author

So, lets close

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

No branches or pull requests

4 participants