简体   繁体   中英

CMake ignoring variable in custom command

disclaimer: I'm a newbie.

I have the following CMakeLists.txt file:

cmake_minimum_required( VERSION 2.8.10.2 )
project( widget  )

# gleaned from the original makefile
add_definitions( -DPJ_IS_BIG_ENDIAN=0 )
add_definitions( -DPJ_IS_LITTLE_ENDIAN=1 )
add_definitions( -DPJ_AUTOCONF=1 )

SET ( SRC_PATH  ${CMAKE_CURRENT_SOURCE_DIR}/src/pj )
SET ( sources ${SRC_PATH}/addr_resolv_sock.c 
          ${SRC_PATH}/file_access_unistd.c 
          ${SRC_PATH}/file_io_ansi.c 
          ${SRC_PATH}/guid_simple.c
          ${SRC_PATH}/log_writer_stdout.c
          ${SRC_PATH}/os_core_unix.c
          ${SRC_PATH}/os_error_unix.c
          ${SRC_PATH}/os_time_unix.c
          ${SRC_PATH}/os_timestamp_common.c
          ${SRC_PATH}/os_timestamp_posix.c
          ${SRC_PATH}/pool_policy_malloc.c
          ${SRC_PATH}/sock_bsd.c
          ${SRC_PATH}/sock_select.c 
          )

SET( outpath ${CMAKE_CURRENT_LIST_DIR} "/lib/" )
message(" outpath = " ${outpath} )

include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include )

add_library( pj ${sources} )

ADD_CUSTOM_COMMAND( TARGET pj POST_BUILD 
    COMMAND ${CMAKE_COMMAND} -E echo "what now?"
    COMMAND ${CMAKE_COMMAND} -E copy libpj.a  "$outpath" 
    )

And for some reason, the value of $outpath in the copy command is ignored. If I put in a hardcoded string, eg, "bubba", it works. If I set $outpath to "bubba" it does not work.

I have tried all known variations on $outpath -- ${outpath} , "$outpath" , "${outpath}" ... but none work.

This has got to be something obvious (to you), but I cannot see it.
All help appreciated!

:bp:

The first issue is the value of outpath . You should avoid leaving a space in the set command, since this will interpret the value as two separate entries, the first being ${CMAKE_CURRENT_LIST_DIR} and the second being /lib/ .

The other issue is simply in the use of add_custom_command . You should favour using "generator expressions" here. From the docs:

Arguments to COMMAND may use "generator expressions" with the syntax "$<...>". Generator expressions are evaluated during build system generation to produce information specific to each build configuration.

Also, you need to create the "/lib" directory before calling cmake -E copy if the destination is a directory (which it is in this case).

Finally, a minor optimisation would be to make use of the cmake -E copy_if_different rather than copy .

So to summarise, you need to do something like:

set( outpath "${CMAKE_CURRENT_LIST_DIR}/lib" )
...
add_custom_command( TARGET pj POST_BUILD 
    COMMAND ${CMAKE_COMMAND} -E make_directory ${outpath}
    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:pj> ${outpath}
)

实际上,这不是直接解决被忽略变量问题的直接方法,但是,如果要定义目标的输出位置,则可能应该使用CMake变量LIBRARY_OUTPUT_PATHEXECUTABLE_OUTPUT_PATH或各个目标上的相应属性进行定义。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM