cmakegitlab-ci

How to determine if cmake is building in a gitlab pipeline or on a local machine


I'm working on a project that builds locally and in a gitlab pipeline. I'd like cmake to

I have many (more than a dozen) libraries that I link into the executable. Here's an example

liba.so
libb.so
libc.so

These libraries are in /usr/lib64 when in a pipeline (installed with dnf)

/usr/lib64/dir_a
/usr/lib64/dir_b
/usr/lib64/dir_c

and in the build directory when built locally

${CMAKE_BINARY_DIR}/lib/dir_a
${CMAKE_BINARY_DIR}/lib/dir_b
${CMAKE_BINARY_DIR}/lib/dir_c

Currently, my link_directories command contains both sets of directories

link_directories(
    ${CMAKE_BINARY_DIR}/lib/dir_a
    ${CMAKE_BINARY_DIR}/lib/dir_b
    ${CMAKE_BINARY_DIR}/lib/dir_c
    /usr/lib64/dir_a
    /usr/lib64/dir_b
    /usr/lib64/dir_c)

If I can determine if cmake is running in a pipeline, I can simplify the cmake commands to something like this

if (${IS_PIPELINE})
    set(LINK_DIRECTORY_PREFIX /usr/lib64)
else()
    set(LINK_DIRECTORY_PREFIX ${CMAKE_BINARY_DIR}/lib)
endif()

link_directories(
    ${LINK_DIRECTORY_PREFIX}/dir_a
    ${LINK_DIRECTORY_PREFIX}/dir_b
    ${LINK_DIRECTORY_PREFIX}/dir_c)

Does anyone know how to automatically detect if cmake is running in a pipeline or on a local machine?


Solution

  • Check if environment variable is set. See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html . Pick one. Typically:

    if (DEFINED ENV{CI})
    

    While I answered your question, I do not agree with the method. You should not check where from you are running inside cmake. Instead, consider your gitlab pipeline to pass a variable to cmake configuration stage to notify where from to load libraries.

    Even better, you could instead just find_library(LIBA liba.so PATHS ${CMAKE_BINARY_DIR}/lib /usr/lib64) automatically.