remymuller / juce-cmake

CMake find module for the JUCE library
MIT License
30 stars 6 forks source link

generate SharedCode target to avoid compiling juce code with each plugin format #10

Open remymuller opened 6 years ago

christofmuc commented 4 years ago

Maybe this is already the issue I was about to file, so let me ask the question like this:

I have a multi-library JUCE project (e.g. you can look at https://github.com/christofmuc/juce-spectroscope19-ci which is public), and compile a few libraries as static link libraries. These libraries use JUCE. With the current find_package() in my CMakeLists for the static library I do

find_package(JUCE REQUIRED 
    COMPONENTS 
        juce_core       
)

add_library(juce-spectroscope19 ${Sources})
target_include_directories(juce-spectroscope19 INTERFACE ${CMAKE_CURRENT_LIST_DIR} PUBLIC ${GLEW_DIRECTORY} "${asiosdk_SOURCE_DIR}/common")
target_link_libraries(juce-spectroscope19 PRIVATE ${JUCE_LIBRARIES})

This will work nicely, but obviously the juce CPP files are now part of this library project and will be compiled and linked into this library. As the main project is also JUCE-based, I do in the top-level CMakeLists:

find_package(JUCE REQUIRED 
    COMPONENTS 
        juce_core       
...more
)

add_executable(JuceSpectroscope19 WIN32 ${SOURCES})
target_include_directories(JuceSpectroscope19 INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(JuceSpectroscope19 PRIVATE ${JUCE_LIBRARIES} glew32 juce-spectroscope19)

which will again add the JUCE cpp files to the executable. Surprisingly, this works despite the same files from juce being present multiple times to the linker, and actually sometimes in some rebuild situations the linker fails with duplicate symbols. But it is not efficient, as I have the JUCE cpp files everywhere.

I would like to only use the JUCE Header generation and set the appropriate target_compile_options, but the variables JUCE_INCLUDE_DIR etc. are not populated. I must be missing something obvious?

I must admit I did not get the comment about the "merge" target that is in the FindJUCE.cmake file.

otristan commented 4 years ago

You can cheat a bit like that.

In all your librairies do that

# Juce
IF(NOT DEFINED JUCE_LIBRARIES)
    find_package(JUCE REQUIRED 
        COMPONENTS
            juce_audio_basics # put all required module for the whole project here
    )
    target_link_libraries(MyLibrary PRIVATE ${JUCE_LIBRARIES})
ENDIF()

target_include_directories(MyLibrary PRIVATE ${JUCE_INCLUDES})

Not perfect but it does the job

christofmuc commented 4 years ago

Many thanks! That works!

The only caveat would be that the top-level project needs to list the subset of all JUCE modules required by the sub-modules, as there is no collection of required modules upfront, but given there are not too many, I easily can live with that!