Skip to content

Commit

Permalink
Add support for generating CMakeLists.txt files
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Sep 1, 2019
1 parent 769ce7d commit fa741a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ examples/build-*-Debug/
*.profdata

examples/*/build.py

CMakeLists.txt
7 changes: 4 additions & 3 deletions src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ opt:
rec: clean
@scons ${SCONSFILEARG} -Q run clang=${clang} zap=${zap} win64=${win64} strict=${strict} debug=${debug} opt=${opt} rec=1 sloppy=${sloppy} std=${std} CXX="${CXX}" CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} run=1 args="${RUN_ARGS} $(strip $(subst --,,$(subst --no-print-directory,,${MAKEFLAGS})))" | sed 's/^scons: //g' | uniq


small:
@scons ${SCONSFILEARG} -Q ${CMD} small=1 clang=${clang} zap=${zap} win64=${win64} std=${std} CXX="${CXX}" CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} | sed 's/^scons: //g' | uniq


tiny:
@scons ${SCONSFILEARG} -Q ${CMD} small=1 tiny=1 clang=${clang} zap=${zap} win64=${win64} std=${std} CXX="${CXX}" CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} | sed 's/^scons: //g' | uniq
@sstrip ${NAME} && echo sstrip ${NAME} || true
Expand Down Expand Up @@ -117,6 +115,9 @@ test:
pro:
@scons ${SCONSFILEARG} -Q pro clang=${clang} zap=${zap} win64=${win64} strict=${strict} debug=${debug} opt=${opt} rec=${rec} sloppy=${sloppy} std=${std} CXX="${CXX}" CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} | sed 's/^scons: //g' | uniq

cmake:
@scons ${SCONSFILEARG} -Q cmake clang=${clang} zap=${zap} win64=${win64} strict=${strict} debug=${debug} opt=${opt} rec=${rec} sloppy=${sloppy} std=${std} CXX="${CXX}" CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} | sed 's/^scons: //g' | uniq

clang:
@scons ${SCONSFILEARG} -Q ${CMD} clang=1 std=${std} CXX=clang++ CXXFLAGS="${CXXFLAGS}" system_include_dir=${system_include_dir} | sed 's/^scons: //g' | uniq

Expand Down Expand Up @@ -323,7 +324,7 @@ script:
ninja:
@test -f CMakeLists.txt || (echo 'error: Could not find CMakeLists.txt' && exit 1)
@-rm -rf build/
@mkdir -p build && cd build && cmake .. -GNinja
@mkdir -p build && cd build && which ccache >/dev/null 2>/dev/null && cmake .. -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja || cmake .. -GNinja
@ninja -C build

ninja_install:
Expand Down
50 changes: 46 additions & 4 deletions src/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,8 +2180,7 @@ def cxx_main():
if name.endswith(".exe"):
name = name[:-4]
if os.path.exists(name + ".pro"):
print("File already exists: " + name + ".pro")
exit(1)
print("overwriting " + name + ".pro")
project_file = open(name + ".pro", "w")
project_file.write("TEMPLATE = app\n\n")
project_file.write("CONFIG += c++2a\n")
Expand All @@ -2203,14 +2202,57 @@ def cxx_main():
s = "DEFINES += "
for define in env['CPPDEFINES']:
if "=" in define:
name, value = define.split("=", 1)
s += name + '="' + value.replace(r'"', r'\"').replace("'\\\"", "'\\\"" + "$$_PRO_FILE_PWD_/") + '" '
key, value = define.split("=", 1)
s += key + '="' + value.replace(r'"', r'\"').replace("'\\\"", "'\\\"" + "$$_PRO_FILE_PWD_/") + '" '
else:
s += define + ' '
project_file.write(s.strip() + "\n")
project_file.close()
exit(0)

# Generate a CMakeLists.txt file if "cmake" is an argument
if 'cmake' in COMMAND_LINE_TARGETS:
name = main_executable
if name.endswith(".exe"):
name = name[:-4]
if os.path.exists("CMakeLists.txt"):
print("Overwriting CMakeLists.txt")
project_file = open("CMakeLists.txt", "w")
project_file.write("# Generated using cxx from https://github.com/xyproto/cxx\n")
project_file.write("cmake_minimum_required(VERSION 3.1)\n")
project_file.write("project(" + name + ")\n")
#project_file.write("set_property(GLOBAL PROPERTY CXX_STANDARD c++2a)\n")
project_file.write("set(TARGET " + name + " PROPERTY CXX_STANDARD c++2a)\n")
project_file.write("set(SOURCES " + " ".join([main_source_file] + sorted(dep_src)) + ")\n")
project_file.write("add_executable(" + name + " ${SOURCES})\n")
if 'LIBS' in env:
project_file.write("target_link_libraries(" + name + " " + " ".join(["-l" + x for x in env['LIBS']]) + ")\n")
#project_file.write("include_directories(" + " ".join(sorted(new_includes)) + ")\n")
project_file.write("target_include_directories(" + name + " PRIVATE " + " ".join(sorted(new_includes)) + ")\n")
if 'CXX' in env:
project_file.write("set(CMAKE_CXX_COMPILER " + env['CXX'] + ")\n")
if 'CC' in env:
project_file.write("set(CMAKE_C_COMPILER " + env['CC'] + ")\n")
if 'CXXFLAGS' in env:
# Don't treat warnings as errors when using QtCreator, since it's good at highlighting warnings by itself
project_file.write("set(CMAKE_CXXFLAGS " + " ".join(env['CXXFLAGS']).replace("-Wfatal-errors ", "") + ")\n")
if 'LINKFLAGS' in env:
project_file.write("set(TARGET " + name + " PROPERTY LINK_FLAGS " + " ".join(env['LINKFLAGS']) + ")\n")
# Also add linkflags with target_link_libraries
project_file.write("target_link_libraries(" + name + " " + " ".join(env['LINKFLAGS']) + ")\n")
if 'CPPDEFINES' in env:
s = "add_definitions("
for define in env['CPPDEFINES']:
if "=" in define:
key, value = define.split("=", 1)
# '"img/"' -> "${CMAKE_CURRENT_SOURCE_DIR/img/"
s += "-D" + key + '="' + value.replace("'\"", "${CMAKE_CURRENT_SOURCE_DIR}/").replace("\"'", "\"") + " "
else:
s += "-D" + define + ' '
project_file.write(s.strip() + ")\n")
project_file.close()
exit(0)

# Set up non-default targets for all the test executables (based on *_test sources)
for test_src in test_sources:
test_elf = os.path.splitext(test_src)[0]
Expand Down

0 comments on commit fa741a6

Please sign in to comment.