Skip to content

Instantly share code, notes, and snippets.

@mathewmariani
Last active August 11, 2019 17:30
Show Gist options
  • Save mathewmariani/7553f863a00807d77602438d94369e13 to your computer and use it in GitHub Desktop.
Save mathewmariani/7553f863a00807d77602438d94369e13 to your computer and use it in GitHub Desktop.
import os
from shutil import copyfile
library_string = ""
root = "src"
header = (".h", ".hpp")
impl = (".c", ".cc", ".cpp")
variables = []
temp_file = open("temp.txt", "w+")
def cmake_set(vars):
for i in vars:
temp_file.write("set(%s %s)\n" %(i, vars[i]))
#end
def cmake_set(var, values):
if isinstance(values, list):
values = "\n"+"\n".join(values)+"\n"
temp_file.write("set(%s %s)\n\n" %(var, values))
#end
def cmake_sourcegroup(var, path):
path = path.replace("\\", "\\\\").replace("/", "\\\\")
temp_file.write("source_group(\"%s\" FILES %s)\n\n" %(path, var))
# end
def cmake_reference(var):
ref = "${%s}" %(var)
return ref
# end
def cmake_comment(string):
temp_file.write("\n#\n# %s\n#\n\n" %(string))
#end
for path, subdirs, files in os.walk(root):
cmake_variable = path.replace("/", "_").replace("\\", "_").upper()
cmake_files = []
cmake_directory = path
cmake_comment(path.replace("/", ".").replace("\\", ".").lower())
for name in files:
# if fnmatch(name, impl) or fnmatch(name, header):
if name.endswith(impl) or name.endswith(header):
cmake_files.extend([os.path.join(path, name).replace("\\", "/")])
if not cmake_files:
continue
# add new variable
variables.extend([cmake_reference(cmake_variable)])
cmake_set(cmake_variable, cmake_files)
cmake_sourcegroup(cmake_reference(cmake_variable), cmake_directory)
# end
# create the library source
cmake_comment("create library")
cmake_set("ENJINE_LIB_SRC", variables)
temp_file.close()
file = open('temp.txt', 'r')
string = file.read()
with open("CMakeLists.txt", "wt") as fout:
with open("template.cmake", "rt") as fin:
for line in fin:
fout.write(line.replace("__library_source__", string))
#end
#os.remove("temp.txt")
file.close()
# General Purpose CMakeLists.txt
# cmake build script
cmake_minimum_required(VERSION 2.8)
# set compiler
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y")
# project
project(someproject)
set(VERSION_MAJOR 0)
set(VERSION_MINOR 0)
set(VERSION_REVIS 0)
set(VERSION_PATCH 0)
set(PROJECT_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVIS}.${VERSION_PATCH})
# modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# binary directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# options
set(REQUIRES_SDL2 false CACHE BOOL "Includes SDL2 to project")
set(REQUIRES_SDL2_IMAGE false CACHE BOOL "Includes SDL2_Image to project")
set(REQUIRES_OPENGL false CACHE BOOL "Includes OpenGL to project")
set(REQUIRES_GLEW false CACHE BOOL "Includes GLEW to project")
set(BUILD_EXE false CACHE BOOL "Adds an executable target (Requires main.cpp).")
# add include directory
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
if(REQUIRES_SDL2)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
endif()
if(REQUIRES_SDL2_IMAGE)
find_package(SDL2_IMAGE REQUIRED)
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
endif()
if(REQUIRES_OPENGL)
find_package(OPENGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
# since glew requires OpenGL
if(REQUIRES_GLEW)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIR})
endif()
endif()
__library_source__
if(BUILD_EXE)
add_executable(${PROJECT_NAME} ${ENJINE_LIB_SRC})
endif()
# installation
if(REQUIRES_SDL2)
if(SDL2_FOUND)
message(STATUS "sdl2 found")
endif()
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
endif()
if(REQUIRES_SDL2_IMAGE)
if(SDL2_IMAGE_FOUND)
message(STATUS "sdl2_image found")
endif()
target_link_libraries(${PROJECT_NAME} ${SDL2_IMAGE_LIBRARY})
endif()
if(REQUIRES_OPENGL)
if(OPENGL_FOUND)
message(STATUS "opengl found")
endif()
if(OPENGL_GLU_FOUND)
message(STATUS "glu found")
endif()
target_link_libraries(${PROJECT_NAME} ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
# since glew requires OpenGL
if(REQUIRES_GLEW)
if(GLEW_FOUND)
message(STATUS "glew found")
endif()
target_link_libraries(${PROJECT_NAME} ${GLEW_LIBRARY})
endif()
endif()
if(APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES OSX_ARCHITECTURES "i386;x86_64;")
endif()
INSTALL(
TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION ${EXECUTABLE_OUTPUT_PATH}
ARCHIVE DESTINATION ${LIBRARY_OUTPUT_PATH}
LIBRARY DESTINATION ${LIBRARY_OUTPUT_PATH}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment