Skip to content

Commit

Permalink
update cmake infra
Browse files Browse the repository at this point in the history
  • Loading branch information
bast committed Dec 18, 2017
1 parent 2921934 commit 3ad86c3
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 59 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is autogenerated by Autocmake v1.0.0-alpha-x http://autocmake.org
# Copyright (c) 2015-2017 by Radovan Bast, Jonas Juselius, and contributors.
# Copyright (c) 2015-2017 by Radovan Bast, Roberto Di Remigio, Jonas Juselius, and contributors.

# set minimum cmake version
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
Expand Down
56 changes: 13 additions & 43 deletions cmake/autocmake/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def check_cmake_exists(cmake_command):
"""
from subprocess import Popen, PIPE

p = Popen('{0} --version'.format(cmake_command),
shell=True,
stdin=PIPE,
stdout=PIPE)
p = Popen(
'{0} --version'.format(cmake_command),
shell=True,
stdin=PIPE,
stdout=PIPE)
if not ('cmake version' in p.communicate()[0].decode('UTF-8')):
sys.stderr.write(' This code is built using CMake\n\n')
sys.stderr.write(' CMake is not found\n')
Expand All @@ -41,41 +42,16 @@ def setup_build_path(build_path):
fname = os.path.join(build_path, 'CMakeCache.txt')
if os.path.exists(fname):
sys.stderr.write('aborting setup\n')
sys.stderr.write('build directory {0} which contains CMakeCache.txt already exists\n'.format(build_path))
sys.stderr.write('remove the build directory and then rerun setup\n')
sys.stderr.write(
'build directory {0} which contains CMakeCache.txt already exists\n'.
format(build_path))
sys.stderr.write(
'remove the build directory and then rerun setup\n')
sys.exit(1)
else:
os.makedirs(build_path, 0o755)


def test_adapt_cmake_command_to_platform():

cmake_command = "FC=foo CC=bar CXX=RABOOF cmake -DTHIS -DTHAT='this and that cmake' .."
res = adapt_cmake_command_to_platform(cmake_command, 'linux')
assert res == cmake_command
res = adapt_cmake_command_to_platform(cmake_command, 'win32')
assert res == "set FC=foo && set CC=bar && set CXX=RABOOF && cmake -DTHIS -DTHAT='this and that cmake' .."

cmake_command = "cmake -DTHIS -DTHAT='this and that cmake' .."
res = adapt_cmake_command_to_platform(cmake_command, 'linux')
assert res == cmake_command
res = adapt_cmake_command_to_platform(cmake_command, 'win32')
assert res == cmake_command


def adapt_cmake_command_to_platform(cmake_command, platform):
"""
Adapt CMake command to MS Windows platform.
"""
if platform == 'win32':
pos = cmake_command.find('cmake')
s = ['set {0} &&'.format(e) for e in cmake_command[:pos].split()]
s.append(cmake_command[pos:])
return ' '.join(s)
else:
return cmake_command


def run_cmake(command, build_path, default_build_path):
"""
Execute CMake command.
Expand All @@ -84,12 +60,7 @@ def run_cmake(command, build_path, default_build_path):
from shutil import rmtree

topdir = os.getcwd()
os.chdir(build_path)
p = Popen(command,
shell=True,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE)
p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout_coded, stderr_coded = p.communicate()
stdout = stdout_coded.decode('UTF-8')
stderr = stderr_coded.decode('UTF-8')
Expand All @@ -104,7 +75,7 @@ def run_cmake(command, build_path, default_build_path):
sys.stderr.write(stderr)

# write cmake output to file
with open('cmake_output', 'w') as f:
with open(os.path.join(build_path, 'cmake_output'), 'w') as f:
f.write(stdout)

# change directory and return
Expand Down Expand Up @@ -164,8 +135,7 @@ def configure(root_directory, build_path, cmake_command, only_show):
if not only_show:
setup_build_path(build_path)

cmake_command = adapt_cmake_command_to_platform(cmake_command, sys.platform)

cmake_command += ' -B' + build_path
print('{0}\n'.format(cmake_command))
if only_show:
sys.exit(0)
Expand Down
6 changes: 3 additions & 3 deletions cmake/autocmake/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def gen_cmake_command(config):
s.append(' command.append({0})'.format(definition))

s.append(" command.append('-DCMAKE_BUILD_TYPE={0}'.format(arguments['--type']))")
s.append(" command.append('-G \"{0}\"'.format(arguments['--generator']))")
s.append(" command.append('-G\"{0}\"'.format(arguments['--generator']))")
s.append(" if arguments['--cmake-options'] != \"''\":")
s.append(" command.append(arguments['--cmake-options'])")
s.append(" if arguments['--prefix']:")
Expand All @@ -40,7 +40,7 @@ def autogenerated_notice():

s = []
s.append('# This file is autogenerated by Autocmake v{0} http://autocmake.org'.format(__version__))
s.append('# Copyright (c) {0} by Radovan Bast, Jonas Juselius, and contributors.'.format(year_range))
s.append('# Copyright (c) {0} by Radovan Bast, Roberto Di Remigio, Jonas Juselius, and contributors.'.format(year_range))

return '\n'.join(s)

Expand Down Expand Up @@ -110,7 +110,7 @@ def gen_setup(config, default_build_type, relative_path, setup_script_name):
s.append("build_path = arguments['<builddir>']")
s.append("\n")
s.append("# create cmake command")
s.append("cmake_command = '{0} {1}'.format(gen_cmake_command(options, arguments), root_directory)")
s.append("cmake_command = '{0} -H{1}'.format(gen_cmake_command(options, arguments), root_directory)")
s.append("\n")
s.append("# run cmake")
s.append("configure.configure(root_directory, build_path, cmake_command, arguments['--show'])")
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_ccache.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Adds ccache support.
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_code_coverage.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Enables code coverage by appending corresponding compiler flags.
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_custom_color_messages.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Colorize CMake output.
Expand Down
6 changes: 4 additions & 2 deletions cmake/downloaded/autocmake_cxx.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Adds C++ support.
Expand All @@ -22,8 +25,7 @@
# docopt:
# - "--cxx=<CXX> C++ compiler [default: g++]."
# - "--extra-cxx-flags=<EXTRA_CXXFLAGS> Extra C++ compiler flags [default: '']."
# export: "'CXX={0}'.format(arguments['--cxx'])"
# define: "'-DEXTRA_CXXFLAGS=\"{0}\"'.format(arguments['--extra-cxx-flags'])"
# define: "'-DCMAKE_CXX_COMPILER={0} -DEXTRA_CXXFLAGS=\"{1}\"'.format(arguments['--cxx'], arguments['--extra-cxx-flags'])"

if(NOT DEFINED CMAKE_CXX_COMPILER_ID)
message(FATAL_ERROR "CMAKE_CXX_COMPILER_ID variable is not defined!")
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_default_build_paths.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Sets binary and library output directories to ${PROJECT_BINARY_DIR}/bin
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_definitions.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Add preprocessor definitions (example: --add-definitions="-DTHIS -DTHAT=137").
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_export_header.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Generates export header for your API using best practices.
Expand Down
6 changes: 4 additions & 2 deletions cmake/downloaded/autocmake_fc_optional.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Adds optional Fortran support.
Expand Down Expand Up @@ -32,9 +35,8 @@
# - "--fc=<FC> Fortran compiler [default: gfortran]."
# - "--extra-fc-flags=<EXTRA_FCFLAGS> Extra Fortran compiler flags [default: '']."
# - "--fc-support=<FC_SUPPORT> Toggle Fortran language support (ON/OFF) [default: ON]."
# export: "'FC={0}'.format(arguments['--fc'])"
# define:
# - "'-DEXTRA_FCFLAGS=\"{0}\"'.format(arguments['--extra-fc-flags'])"
# - "'-DCMAKE_Fortran_COMPILER={0} -DEXTRA_FCFLAGS=\"{1}\"'.format(arguments['--fc'], arguments['--extra-fc-flags'])"
# - "'-DENABLE_FC_SUPPORT={0}'.format(arguments['--fc-support'])"

option(ENABLE_FC_SUPPORT "Enable Fortran language support" ON)
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_python_interpreter.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Detects Python interpreter.
Expand Down
3 changes: 3 additions & 0 deletions cmake/downloaded/autocmake_safeguards.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# (c) https://github.com/coderefinery/autocmake/blob/master/AUTHORS.md
# licensed under BSD-3: https://github.com/coderefinery/autocmake/blob/master/LICENSE

#.rst:
#
# Provides safeguards against in-source builds and bad build types.
Expand Down
16 changes: 15 additions & 1 deletion cmake/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@
AUTOCMAKE_GITHUB_URL = 'https://github.com/coderefinery/autocmake/raw/master/'


def licensing_info():
return '''The CMake infrastructure for this project is generated using [Autocmake]
by Radovan Bast, Roberto Di Remigio, Jonas Juselius and contributors.
The `update.py` Python script and the contents of the directories `autocmake` and `downloaded` are licensed
under the terms of the [BSD-3-Clause license], unless otherwise stated.
[Autocmake]: http://autocmake.org
[BSD-3-Clause license]: https://tldrlegal.com/license/bsd-3-clause-license-(revised)'''


def check_for_yaml():
try:
import yaml
except:
except ImportError:
sys.stderr.write("ERROR: you need to install the pyyaml package\n")
sys.exit(-1)

Expand Down Expand Up @@ -257,6 +267,10 @@ def main(argv):
src='{0}{1}'.format(AUTOCMAKE_GITHUB_URL, f),
dst='{0}'.format(f)
)
# finally create a README.md with licensing information
with open('README.md', 'w') as f:
print('- generating licensing information')
f.write(licensing_info())
sys.exit(0)

process_yaml(argv)
Expand Down
12 changes: 5 additions & 7 deletions setup
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python

# This file is autogenerated by Autocmake v1.0.0-alpha-x http://autocmake.org
# Copyright (c) 2015-2017 by Radovan Bast, Jonas Juselius, and contributors.
# Copyright (c) 2015-2017 by Radovan Bast, Roberto Di Remigio, Jonas Juselius, and contributors.

import os
import sys
Expand Down Expand Up @@ -49,12 +49,10 @@ def gen_cmake_command(options, arguments):
Generate CMake command based on options and arguments.
"""
command = []
command.append('FC={0}'.format(arguments['--fc']))
command.append('CXX={0}'.format(arguments['--cxx']))
command.append(arguments['--cmake-executable'])
command.append('-DEXTRA_FCFLAGS="{0}"'.format(arguments['--extra-fc-flags']))
command.append('-DCMAKE_Fortran_COMPILER={0} -DEXTRA_FCFLAGS="{1}"'.format(arguments['--fc'], arguments['--extra-fc-flags']))
command.append('-DENABLE_FC_SUPPORT={0}'.format(arguments['--fc-support']))
command.append('-DEXTRA_CXXFLAGS="{0}"'.format(arguments['--extra-cxx-flags']))
command.append('-DCMAKE_CXX_COMPILER={0} -DEXTRA_CXXFLAGS="{1}"'.format(arguments['--cxx'], arguments['--extra-cxx-flags']))
command.append('-DPYTHON_INTERPRETER="{0}"'.format(arguments['--python']))
command.append('-DUSE_CCACHE={0}'.format(arguments['--ccache']))
command.append('-DCMAKE_INSTALL_BINDIR={0}'.format(arguments['--bindir']))
Expand All @@ -66,7 +64,7 @@ def gen_cmake_command(options, arguments):
command.append('-DPREPROCESSOR_DEFINITIONS="{0}"'.format(arguments['--add-definitions']))
command.append('-DENABLE_CODE_COVERAGE={0}'.format(arguments['--coverage']))
command.append('-DCMAKE_BUILD_TYPE={0}'.format(arguments['--type']))
command.append('-G "{0}"'.format(arguments['--generator']))
command.append('-G"{0}"'.format(arguments['--generator']))
if arguments['--cmake-options'] != "''":
command.append(arguments['--cmake-options'])
if arguments['--prefix']:
Expand Down Expand Up @@ -97,7 +95,7 @@ build_path = arguments['<builddir>']


# create cmake command
cmake_command = '{0} {1}'.format(gen_cmake_command(options, arguments), root_directory)
cmake_command = '{0} -H{1}'.format(gen_cmake_command(options, arguments), root_directory)


# run cmake
Expand Down

0 comments on commit 3ad86c3

Please sign in to comment.