简体   繁体   中英

Running a custom command in Cmake

I am very new to Cmake and need to generate some files at compile time. once generated i need to compile and link the files. I ve created the cmake makefile to compile the already generated files like

cmake_minimum_required(VERSION 2.6)
project(demo)
set(CMAKE_CXX_FLAGS "-DWITH_COOKIES")
add_library(soapC soapC.cpp soapVimBindingProxy.cpp)
add_library(stdsoap2  /home/abdullah/installs/gsoap-shah_edits/gsoap/stdsoap2.cpp)
add_executable(demo test_file.cc test_app.cc)
target_link_libraries(demo soapC stdsoap2 gsoap++)

This successfully compiles the project. However the files soapC.cpp soapVimBindingProxy.cpp needs to be generated first. And I want to generate these files at runtime using the gsoap tool.

following is the command that needs to be run to generate the header file

wsdl2h -o outfile.h infile.wsdl

This takes an input wsdl file and creates a corresponding outfile.h. Now I tried doing this in cmake like this

cmake_minimum_required(VERSION 2.6)
add_custom_command(
OUTPUT vsphere.h
COMMAND wsdl2h -o vsphere.h vim25/vim.wsdl
) 

But something goes wrong here. No error pops up but no file is created either. Am I missing something ? All help much appreciated. Thanks.

You've just created a command for producing your header file, so CMake knows just where to get vsphere.h from. I'd recommend using OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h in the add_custom_command() call.

Now you need to create a target:

add_custom_target(vsphere_header ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/vsphere.h)

Finally, make your soapC target depend on it:

add_dependencies(soapC vsphere_header)

Be sure to place add_dependencies() call after soapC target definition.

Thanks arrododger and TobyHijzen for your lighting on this issue.

I use add_custom_command with main_dependency feature for solution for this issue. Following is my CMakeLists.txt for famous calc example of gsoap tutorial.

cmake_minimum_required(VERSION 2.8)

# Proejct name
PROJECT(Calculator)

# Make verbose level on/off
SET(CMAKE_VERBOSE_MAKEFILE ON)

# Varialbes used in cmake
SET(TARGET calc_client)
SET(GSOAP_STATIC_LIB gsoap)

SET(CLIENT_SRC calc_client.c)

SET(WSDL2H_EXEC wsdl2h)
SET(WSDL2H_IN http://www.genivia.com/calc.wsdl)
#SET(WSDL2H_IN calc.wsdl)
SET(WSDL2H_OUT calc.h)
SET(WSDL2H_OPT -c -o)

# command for generating stub and xml serializer code
ADD_CUSTOM_COMMAND(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${WSDL2H_OUT}
    COMMAND ${WSDL2H_EXEC} -c -ttypemap.dat -o ${WSDL2H_OUT} ${WSDL2H_IN}
    COMMENT "=================== Generating ${WSDL2H_OUT} gSOAP Header file ..."
)

SET(SOAPCPP2_EXEC soapcpp2)
SET(STUB soapClient.c soapC.c)

SET(SOAPCPP2_OUT
    ${STUB}
    calc.add.req.xml
    calc.add.res.xml
    calc.sub.req.xml
    calc.sub.res.xml
    calc.mul.req.xml
    calc.mul.res.xml
    calc.div.res.xml
    calc.div.req.xml
    calc.pow.res.xml
    calc.pow.req.xml
    calc.nsmap
    soapH.c
    soapH.h
    soapStub.h
    soapClientLib.c
)
# command for generating stub and xml serializer code
ADD_CUSTOM_COMMAND(
    OUTPUT ${STUB}
    COMMAND ${SOAPCPP2_EXEC} -c -C ${WSDL2H_OUT}
    MAIN_DEPENDENCY ${WSDL2H_OUT}
    COMMENT "=================== Generating ${STUB} outputs ..."
)


# Exutable files and dependents
ADD_EXECUTABLE(${TARGET} ${CLIENT_SRC} ${STUB})

# libraries for taget : for gcc -l option
TARGET_LINK_LIBRARIES(${TARGET} ${GSOAP_STATIC_LIB})

# Compiler options
ADD_DEFINITIONS(-Wall -O2 -s)

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