cmake:如何在外部项目中正确设置变量以先于 make?
cmake: How do I correctly set variables to precede make in an external project?
我正在为我在 pi 项目中需要的库设置一个交叉编译项目。我想通过最新的 mosquitto 库,我已经弄清楚我需要传递什么才能正确构建它。不幸的是,当我定义我的 BUILD_COMMAND 时,我似乎无法在调用之前正确设置变量。
这里是我定义的外部项目CMakeLists.txt:
ExternalProject_Add(mosquitto
URL ${SRC_URL} URL_MD5 ${SRC_MD5}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND echo "No configuration necessary."
BUILD_COMMAND cd <SOURCE_DIR>/lib && export CC=gcc && export CXX=g++ && export CROSS_COMPILE=${CROSS_COMPILE_TRIPLE} && export CFLAGS=--sysroot=${CMAKE_SYSROOT} && export LDFLAGS="--sysroot=${CMAKE_SYSROOT} -Wl,-rpath-link,${CMAKE_SYSROOT}/lib/arm-linux-gnueabihf -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/arm-linux-gnueabihf -L${CMAKE_SYSROOT}/usr/lib/arm-linux-gnueabihf" && make --trace
这是失败步骤的 make 输出:
arm-linux-gnueabihf-gcc -shared "--sysroot=/home/heardg/pi/system/devroot -Wl,-rpath-link,/home/heardg/pi/system/devroot/lib/arm-linux-gnueabihf -Wl,-rpath-link,/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf -L/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf" -Wl,--version-script=linker.version -Wl,-soname,libmosquitto.so.1 mosquitto.o logging_mosq.o memory_mosq.o messages_mosq.o net_mosq.o read_handle.o read_handle_client.o read_handle_shared.o send_mosq.o send_client_mosq.o socks_mosq.o srv_mosq.o thread_mosq.o time_mosq.o tls_mosq.o util_mosq.o will_mosq.o -o libmosquitto.so.1 -lrt -lssl -lcrypto -lpthread -lcares
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find crti.o: No such file or directory
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lrt
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lssl
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcrypto
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpthread
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcares
collect2: error: ld returned 1 exit status
Makefile:46: recipe for target 'libmosquitto.so.1' failed`
我发现导致问题的是 LDFLAGS 周围的引号。
"--sysroot=/home/heardg/pi/system/devroot -Wl,-rpath-link,/home/heardg/pi/system/devroot/lib/arm-linux-gnueabihf -Wl,-rpath-link,/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf -L/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf"
如果我手动 运行 link 命令并删除上面的双引号,它就会成功。
我怎样才能更好地形成 BUILD_COMMAND 中的参数来去掉引号?
当 ExternalProject_Add
有很多命令时,最好使用脚本来执行它们。至于将 CMake 变量传递给脚本,它们可以通过脚本的参数传递,也可以通过脚本本身的配置传递:
build_mosquitto.sh.in:
# The only argument to the script is mosquitto's source directory.
source_dir=
exports CC=gcc
export CXX=g++
export CROSS_COMPILE=@CROSS_COMPILE_TRIPLE@
# ... other exports
cd ${source_dir}/lib && make
CMakeLists.txt:
configure_file("build_mosquitto.sh.in" "build_mosquitto.sh" @ONLY)
ExternalProject_Add(mosquitto
URL ${SRC_URL} URL_MD5 ${SRC_MD5}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND echo "No configuration necessary."
BUILD_COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/build_mosquitto.sh" <SOURCE_DIR>
)
我正在为我在 pi 项目中需要的库设置一个交叉编译项目。我想通过最新的 mosquitto 库,我已经弄清楚我需要传递什么才能正确构建它。不幸的是,当我定义我的 BUILD_COMMAND 时,我似乎无法在调用之前正确设置变量。
这里是我定义的外部项目CMakeLists.txt:
ExternalProject_Add(mosquitto
URL ${SRC_URL} URL_MD5 ${SRC_MD5}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND echo "No configuration necessary."
BUILD_COMMAND cd <SOURCE_DIR>/lib && export CC=gcc && export CXX=g++ && export CROSS_COMPILE=${CROSS_COMPILE_TRIPLE} && export CFLAGS=--sysroot=${CMAKE_SYSROOT} && export LDFLAGS="--sysroot=${CMAKE_SYSROOT} -Wl,-rpath-link,${CMAKE_SYSROOT}/lib/arm-linux-gnueabihf -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/arm-linux-gnueabihf -L${CMAKE_SYSROOT}/usr/lib/arm-linux-gnueabihf" && make --trace
这是失败步骤的 make 输出:
arm-linux-gnueabihf-gcc -shared "--sysroot=/home/heardg/pi/system/devroot -Wl,-rpath-link,/home/heardg/pi/system/devroot/lib/arm-linux-gnueabihf -Wl,-rpath-link,/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf -L/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf" -Wl,--version-script=linker.version -Wl,-soname,libmosquitto.so.1 mosquitto.o logging_mosq.o memory_mosq.o messages_mosq.o net_mosq.o read_handle.o read_handle_client.o read_handle_shared.o send_mosq.o send_client_mosq.o socks_mosq.o srv_mosq.o thread_mosq.o time_mosq.o tls_mosq.o util_mosq.o will_mosq.o -o libmosquitto.so.1 -lrt -lssl -lcrypto -lpthread -lcares
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find crti.o: No such file or directory
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lrt
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lssl
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcrypto
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpthread
/home/heardg/pi/linaro/gcc-linaro-4.9-2016.02-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcares
collect2: error: ld returned 1 exit status
Makefile:46: recipe for target 'libmosquitto.so.1' failed`
我发现导致问题的是 LDFLAGS 周围的引号。
"--sysroot=/home/heardg/pi/system/devroot -Wl,-rpath-link,/home/heardg/pi/system/devroot/lib/arm-linux-gnueabihf -Wl,-rpath-link,/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf -L/home/heardg/pi/system/devroot/usr/lib/arm-linux-gnueabihf"
如果我手动 运行 link 命令并删除上面的双引号,它就会成功。
我怎样才能更好地形成 BUILD_COMMAND 中的参数来去掉引号?
当 ExternalProject_Add
有很多命令时,最好使用脚本来执行它们。至于将 CMake 变量传递给脚本,它们可以通过脚本的参数传递,也可以通过脚本本身的配置传递:
build_mosquitto.sh.in:
# The only argument to the script is mosquitto's source directory.
source_dir=
exports CC=gcc
export CXX=g++
export CROSS_COMPILE=@CROSS_COMPILE_TRIPLE@
# ... other exports
cd ${source_dir}/lib && make
CMakeLists.txt:
configure_file("build_mosquitto.sh.in" "build_mosquitto.sh" @ONLY)
ExternalProject_Add(mosquitto
URL ${SRC_URL} URL_MD5 ${SRC_MD5}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND echo "No configuration necessary."
BUILD_COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/build_mosquitto.sh" <SOURCE_DIR>
)