0

I've written a function that I like for downloading and building an external resource. However, as a practical matter, I'm hitting a scope problem with cmake functions and I'd like to expose variables outside of the function. I've already tried GLOBAL PROPERTY, PARENT_SCOPE, CACHE STRING and CACHE INTERNAL without success. How might I update the "global" variables within the function below?

function(DownloadExternal name source options lib_path)
    string(TOUPPER "${parent}" PARENT)
    MESSAGE(STATUS "${name}")
    MESSAGE(STATUS "     source: ${source}")
    MESSAGE(STATUS "     options: ${options}")
    MESSAGE(STATUS "     lib_path: ${lib_path}")

    set(${name}_prefix      ${EXTERNAL_FOLDER}/${name})
    set(${name}_install_dir ${${name}_prefix})
    list(APPEND ${name}_cmake_args
            ${DEFAULT_CMAKE_FLAGS}
            -DCMAKE_INSTALL_PREFIX=${${name}_prefix}
            -DBUILD_SHARED_LIBS=OFF
            -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
            -DBUILD_SHARED_LIBS=FALSE
            ${options}
            )

    # GLOBAL Variable here is only updated locally
    #  Attempted GLOBAL PROPERTY, CACHE INTERNAL/STRING, PARENT_SCOPE
    list(APPEND EXTRA_LIBS ${${name}_install_dir}/lib/${lib_path})
    externalproject_add(${name}
            PREFIX             ${${name}_prefix}
            GIT_REPOSITORY     ${source}
            GIT_SHALLOW        1
            DOWNLOAD_DIR       ${${name}_prefix}
            UPDATE_COMMAND     git pull
            INSTALL_DIR        ${${name}_install_dir}
            CMAKE_ARGS         ${${name}_cmake_args}
            )
    include_directories(${${name}_prefix}/include)
    link_directories(${${name}_prefix}/lib)
    MESSAGE(STATUS "     prefix: ${${name}_prefix}")
    MESSAGE(STATUS "     cmake: ${${name}_cmake_args}")
    MESSAGE(STATUS "     install: ${${name}_install_dir}")
    MESSAGE(STATUS "     libs: ${EXTRA_LIBS}")
endfunction(DownloadExternal)

Usage (this is two lines too long. I want to add the dependencies within the function):

# ----------------------------------------------------------------------------------------------------------------------
#  harfbuzz - A Freetype dependency
# ----------------------------------------------------------------------------------------------------------------------
DownloadExternal(harfbuzz https://github.com/harfbuzz/harfbuzz "-DHB_HAVE_FREETYPE=ON")
ExternalProject_Get_Property(harfbuzz install_dir)
set(harfbuzz_install_dir ${install_dir})

Later Usage:

add_executable(${CMAKE_PROJECT_NAME} ${${PROJECT_NAME}_SOURCE_FILES})

# TODO: Find a way to make this a list
add_dependencies(freetype harfbuzz)
add_dependencies(${CMAKE_PROJECT_NAME} freetype)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

#  TODO: Find a way to add these in the function
target_link_libraries(${CMAKE_PROJECT_NAME}
        ${harfbuzz_install_dir}/lib/libharfbuzz.a
        )

What I want to be able to do is more or less this:

# ----------------------------------------------------------------------------------------------------------------------
#  harfbuzz - A Freetype dependency
# ----------------------------------------------------------------------------------------------------------------------
DownloadExternal(harfbuzz https://github.com/harfbuzz/harfbuzz "-DHB_HAVE_FREETYPE=ON" libharfbuzz.a)

...snip...

target_link_libraries(${CMAKE_PROJECT_NAME}
        ${EXTRA_LIBS}
        )
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55
  • Possible duplicate of [cmake, lost in the concept of global variables (and PARENT\_SCOPE or add\_subdirectory alternatives)](https://stackoverflow.com/questions/19345930/cmake-lost-in-the-concept-of-global-variables-and-parent-scope-or-add-subdirec) – John Apr 24 '18 at 18:17

2 Answers2

1

You can use extra_libs an in/out parameter of DownloadExternal

function(DownloadExternal name source options lib_path extra_list)
        ...
        set(tmpExtra_list ${extra_list} NEW_STUFF_TO_ADD)
        set(${extra_list} ${tmpExtra_list} PARENT_SCOPE) # return value
endfunction()

Pros:

  • Known concept in many programming language
  • No magic where the variable comes from
  • User of your function can define it's one name for the variable

Contras:

  • Signature of your function has to be modified
Th. Thielemann
  • 2,592
  • 1
  • 23
  • 38
0

I believe this works.

A global property must be defined near the top of the cmake...

DEFINE_PROPERTY(GLOBAL PROPERTY EXTRA_LIBS
        BRIEF_DOCS "Contains linkable libraries"
        FULL_DOCS "Contains paths to libraries for linking")

Then within the function, we must set a local variable to the contents of the global property. Additionally, update the local variable and then add it back into the global property.

function(DownloadExternal name source options lib_path)
    string(TOUPPER "${parent}" PARENT)
    MESSAGE(STATUS "---------------------------------------------------------------------------")
    MESSAGE(STATUS "${name}")
    MESSAGE(STATUS "     source: ${source}")
    MESSAGE(STATUS "     options: ${options}")
    MESSAGE(STATUS "     lib_path: ${lib_path}")
    GET_PROPERTY(extra_libs GLOBAL PROPERTY EXTRA_LIBS)
    set(${name}_prefix      ${EXTERNAL_FOLDER}/${name})
    set(${name}_install_dir ${${name}_prefix})
    list(APPEND ${name}_cmake_args
            ${DEFAULT_CMAKE_FLAGS}
            -DCMAKE_INSTALL_PREFIX=${${name}_prefix}
            -DBUILD_SHARED_LIBS=OFF
            -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
            -DBUILD_SHARED_LIBS=FALSE
            ${options}
            )
    list(APPEND extra_libs ${${name}_install_dir}/lib/${lib_path})
    SET_PROPERTY(GLOBAL PROPERTY EXTRA_LIBS ${extra_libs})
    externalproject_add(${name}
            PREFIX             ${${name}_prefix}
            GIT_REPOSITORY     ${source}
            GIT_SHALLOW        1
            DOWNLOAD_DIR       ${${name}_prefix}
            UPDATE_COMMAND     git pull
            INSTALL_DIR        ${${name}_install_dir}
            CMAKE_ARGS         ${${name}_cmake_args}
            )
    include_directories(${${name}_prefix}/include)
    link_directories(${${name}_prefix}/lib)
    MESSAGE(STATUS "     prefix: ${${name}_prefix}")
    MESSAGE(STATUS "     cmake: ${${name}_cmake_args}")
    MESSAGE(STATUS "     install: ${${name}_install_dir}")
    MESSAGE(STATUS "     libs: ${extra_libs}")
endfunction(DownloadExternal)

Finally, to use the property later, we must also get it again.

GET_PROPERTY(extra_libs GLOBAL PROPERTY EXTRA_LIBS)
target_link_libraries(${CMAKE_PROJECT_NAME}
        ${BZIP2_LIBRARIES}
        ${OPENGL_LIBRARIES}
        ${extra_libs}
        )
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55