Search code examples
cmakecmakelists-options

What does -t option in cmake --build?


I have some question about Cmake options.

  1. What does it mean of cmake --build . -j -t label_image? I wonder what -t option means.
  2. Is it same cmake ./ -DTFLITE_ENABLE_GPU=ON with set($TFLITE_ENABLE_GPU ON)? In other words, is the option value of cmake same with the value defined by set function in CmakeLists.txt?

Solution

    1. With -t <tgt> or --target <tgt> you tell cmake to build only the target <tgt> instead of the all target, see the CMake documentation:

    --target <tgt>..., -t <tgt>...

    Build instead of the default target. Multiple targets may be given, separated by spaces.

    1. The alternatives are not the same, even though you may see similar effects in certain scenarios. (I assume the $ in the cmake command to be a typo; otherwise you're not even working with variables of the same name, since the cmake variable name contains the dollar char.)
    set(TFLITE_ENABLE_GPU ON)
    

    Sets a cmake variable. The value of the variable can only be seen in the current scope and descendant scopes. The value won't be visible before this command and it is not available in the parent directory.

    cmake  -DTFLITE_ENABLE_GPU=ON .
    

    on the other hand (re)configures a project in the given directory setting the TFLITE_ENABLE_GPU cache variable to ON. This value is set before cmake starts parsing the CMakeLists.txt files and it is persisted across reconfigurations.

    You could e.g. observe the difference, if you add the following logic to your CMakeLists.txt:

    message("(1) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}; BAR = ${BAR}")
    
    function(test_funct) # functions introduce a new scope
        set(FOO BAR)
        message("(2) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
    function()
    
    test_funct()
    
    message("(3) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
    set(FOO BAZ)
    
    function(test_funct2) # functions introduce a new scope
        message("(4) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
        set(FOO FOOBAR)
        message("(5) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
    function()
    
    test_funct2()
    message("(6) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
    set(FOO 99 CACHE STRING "")
    message("(7) FOO = ${FOO}; CACHE{FOO} = $CACHE{FOO}")
    

    Output of

    cmake -D FOO=42 -S source_dir -B build_dir
    
    (1) FOO = 42; CACHE{FOO} = 42; BAR = 
    (2) FOO = BAR; CACHE{FOO} = 42
    (3) FOO = 42; CACHE{FOO} = 42
    (4) FOO = BAZ; CACHE{FOO} = 42
    (5) FOO = FOOBAR; CACHE{FOO} = 42
    (6) FOO = BAZ; CACHE{FOO} = 42
    (7) FOO = BAZ; CACHE{FOO} = 42
    

    Output when running

    cmake build_dir
    

    after running the command above.

    (1) FOO = 42; CACHE{FOO} = 42; BAR = 
    (2) FOO = BAR; CACHE{FOO} = 42
    (3) FOO = 42; CACHE{FOO} = 42
    (4) FOO = BAZ; CACHE{FOO} = 42
    (5) FOO = FOOBAR; CACHE{FOO} = 42
    (6) FOO = BAZ; CACHE{FOO} = 42
    (7) FOO = BAZ; CACHE{FOO} = 42
    

    Value when running

    cmake -D FOO=44 build_dir
    
    (1) FOO = 44; CACHE{FOO} = 44; BAR = 
    (2) FOO = BAR; CACHE{FOO} = 44
    (3) FOO = 44; CACHE{FOO} = 44
    (4) FOO = BAZ; CACHE{FOO} = 44
    (5) FOO = FOOBAR; CACHE{FOO} = 44
    (6) FOO = BAZ; CACHE{FOO} = 44
    (7) FOO = BAZ; CACHE{FOO} = 44
    

    When running

    cmake -U FOO build_dir
    

    to clear the cache entry, we'll get

    (1) FOO = ; CACHE{FOO} = ; BAR = 
    (2) FOO = BAR; CACHE{FOO} = 
    (3) FOO = ; CACHE{FOO} = 
    (4) FOO = BAZ; CACHE{FOO} = 
    (5) FOO = FOOBAR; CACHE{FOO} = 
    (6) FOO = BAZ; CACHE{FOO} = 
    (7) FOO = BAZ; CACHE{FOO} = 99
    

    Value when removing the cache variable when reconfiguring.