I am attempting to use CMake to generate an OpenGL project template. My set up requires the GLFW static library https://github.com/glfw/glfw ; GLFW is also built using CMake.
I would like to generate and link this library within the configure step of my project. I have gotten my CMakeLists.txt to clone it from github, but I am having trouble get it to build within the script. My idea was to go through the following steps:
- clone the library
- select a specific commit
- configure the glfw cmake files in a build directory
- build the glfw library
- copy the library to an appropriate location and set it up to be used.
However, step 3 and 4 seem to be failing when done within a CMakeLists.txt. I can manually navigate to the build folder for glfw, type the following in a terminal:
cmake ..
cmake --build . --config Release
Typing the above builds the library. But doing the same commands in my CMakeLists.txt does not have the same results, it appears to skip the configure step, and then the cmake--build . --config release step fails with Error: could not load cache
Perhaps this isn't the best workflow, and I am open to suggestions. I am relatively inexperienced with writing CMakeLists.txt. Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(OpenGLBasedApp VERSION 1.0)
#########################################################################################################
# GLFW
#########################################################################################################
#-----clone GLFW repository-----
set(glfw_dir_rel "./third_party/git_repos/glfw")
message(STATUS "running command:" ${git_clone_glfw_cmd})
execute_process(
COMMAND git clone https://github.com/glfw/glfw.git ${glfw_dir_rel}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE git_glfw_clone_output
RESULT_VARIABLE git_glfw_clone_result
ERROR_QUIET
)
# message(STATUS "clone return status: ${git_glfw_clone_result}") 0 on success, 128 on already cloned
#-----switch to specific commit; change this if you want latest version-----
set(supported_glfw_commit "8d7e5cdb49a1a5247df612157ecffdd8e68923d2")
set(glfw_dir_abs "${PROJECT_SOURCE_DIR}${glfw_dir_rel}")
message(STATUS "${glfw_dir_abs}")
#if(git_glfw_clone_result EQUAL "0")
message(STATUS "glfw clone successful -- checking out stable commit ${supported_glfw_commit}")
execute_process(
COMMAND git checkout ${supported_glfw_commit}
WORKING_DIRECTORY "${glfw_dir_abs}"
OUTPUT_VARIABLE glfw_checkout_std_out
RESULT_VARIABLE glfw_checkout_ret_val
#ERROR_QUIET
)
#message(STATUS ::: ${glfw_checkout_std_out})
#message(STATUS ::: ${glfw_checkout_ret_val})
#endif()
# ----- build GLFW to use -----
#the output location of this library may be playform dependent and should be tested
#make the directory for build; relative path seems to be required as directory does not show up with concatenated path
set(glfw_build_dir_rel "${glfw_dir_rel}/build")
file(MAKE_DIRECTORY "${glfw_build_dir_rel}")
message(STATUS "building GLFW, this may take a moment...")
execute_process(
COMMAND cmake ..
COMMAND cmake --build . --config Release
WORKING_DIRECTORY "${glfw_build_dir_rel}"
OUTPUT_VARIABLE glfw_build_out
RESULT_VARIABLE glfw_build_ret
#ERROR_QUIET
)
message(STATUS "glfw status: \n ${glfw_build_ret}")
message(STATUS "glfw build: \n ${glfw_build_out}")
#########################################################################################################
# Main Application
#########################################################################################################
file(GLOB_RECURSE app_files_var
"src/*.h"
"src/*.c"
"src/*.hpp"
"src/*.cpp"
)
add_executable(App ${app_files_var})
I have not attempted step 5 yet because I cannot get it to successfully build. Any help would be appreciated.
edit: ultimately this is a problem because I'm trying to pull from github then build. A proper way to do this is probably to use FetchContent in cmake. I found an example of that here https://github.com/Hoshiningen/OpenGL-Template