0

I need to invoke cmake from within cmake so that I can have binaries built before project files are generated. I have the following CmakeLists.txt:

cmake_minimum_required(VERSION 3.2)
project(StarEngine)

set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

find_package(OpenGL REQUIRED) 
include_directories(${OPENGL_INCLUDE_DIR})

#copy the other cmake file into where we'd like to invoke cmake 
configure_file(deps-CMakeLists.txt deps/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} . WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps)

#eventually binaries will be built, for now this doesn't accomplish anything
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/deps)

And its friend, deps-CmakeLists.txt, a test script:

set(GENERATED_DIR "test")
MESSAGE( STATUS ${GENERATED_DIR} )

In the following file structure:

Project
    build
    Code
        CmakeLists.txt
        deps-CmakeLists.txt

No matter what I put in for the variable value, it is blank when displayed in MESSAGE. I imagine this is weird behavior resulting from invoking cmake from cmake. I had a bunch of other strange errors to, but I suspect if I can figure out this one that will help crack them all.

Christopher Brown
  • 177
  • 1
  • 3
  • 12
  • What do you want to achieve? Why do you pass a CMakeLists.txt to `configure_file`? Your question does not make any sense. Please clarify. – usr1234567 Apr 28 '16 at 04:41
  • `configure_file ()` does replace variables found in the source code file. Just add `COPYONLY` option or use `file(COPY ...)` command. – Florian Apr 28 '16 at 05:50
  • Thanks for the additional information. I think what you are trying to do, doesn't require `execute_process()`. Just adding a dependency would be enough. Could you please add the `CMakeLists.txt` parts where you want to use the binaries and the `deps-CMakeLists.txt` parts where you build them? A more generic way - including the possibility to cross-compile - is described in [How to instruct CMake to use the build architecture compiler?](http://stackoverflow.com/questions/36173840/how-to-instruct-cmake-to-use-the-build-architecture-compiler). – Florian Apr 28 '16 at 07:37
  • It might or might not be relevant to your particular situation, but there's a [related question and answer](http://stackoverflow.com/q/36084785/1938798) which may give you ideas for another approach. In short, during the CMake run, it invokes a nested cmake build to create an executable which is then used in the main CMake run to generate sources and even other CMakeLists.txt files to be added via add_subdirectory(). Not trivial, but I've had it working on a large cross platform project for a little while now. – Craig Scott Apr 28 '16 at 12:04

1 Answers1

0

Thanks to @Florian, the problem was with variable replacement and I needed to add the COPYONLY option to configure_file

jpo38
  • 20,821
  • 10
  • 70
  • 151
Christopher Brown
  • 177
  • 1
  • 3
  • 12
  • You're welcome. Just one thing about `COPYONLY`: it may not automatically recognize changes in the source file. So better use `@ONLY`, then the source file is tracked and will trigger a CMake reconfiguration when changed. – Florian May 02 '16 at 11:11