by krapp · 3y ago · 0 comments
set(PROJECT "ProjectTemplate") #this can't have spaces
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_BUILD_TYPE "Debug")
set(ARCH "x64")
set(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/project")
set(PROJECT_SRC "${PROJECT_ROOT}/src")
set(PROJECT_INC "${PROJECT_ROOT}/include")
set(PROJECT_ASSETS "${PROJECT_ROOT}/assets")
set(PROJECT_LIBS "${PROJECT_ROOT}/libs") #third party libraries
set(SDL2_PATH "${PROJECT_LIBS}/SDL")
project(${PROJECT} C)
add_executable(${PROJECT} "${PROJECT_SRC}/main.c")
set(PROJECT_SOURCE_FILES "${PROJECT_SRC}/main.c")
target_sources(${PROJECT} PUBLIC ${PROJECT_SOURCE_FILES})
set(SDL2_INCLUDE_DIR "${SDL2_PATH}/include")
target_include_directories(${PROJECT} PUBLIC ${PROJECT_INC}
${SDL2_INCLUDE_DIR})
set(SDL2_LIBRARY "${SDL2_PATH}/lib/${ARCH}")
target_link_libraries(${PROJECT} "${SDL2_LIBRARY}/SDL2.lib"
"${SDL2_LIBRARY}/SDL2main.lib")
file(GLOB_RECURSE PROJECT_DLLS "${SDL2_LIBRARY}/SDL2.dll")
add_custom_command(TARGET ${PROJECT} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PROJECT_DLLS}
"$<TARGET_FILE_DIR:${PROJECT}>")
if(EXISTS ${PROJECT_ASSETS})
add_custom_command(TARGET ${PROJECT} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_ASSETS}
"$<TARGET_FILE_DIR:${PROJECT}>/assets")
endif()
Is this the way it's supposed to be done? No. Is it setting someone's teeth on edge? Probably. Is it cross-platform compatible? No, but I don't need it to be, I just need it to build for the specific platform I'm using, which is Windows. Will it build? Yes, and that's all that matters.But I agree with the "keep things simple" (e.g. don't define custom functions), but I don't think that your snippet does that (ahem, I see `add_custom_command` there) :)
It's worth it when you're working on assets or lua scripts and want to import any changes for each rebuild.