hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 1 | #!/usr/bin/python2.7 |
| 2 | |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 3 | # to change compilers under linux use 'export CXX=clang++' or 'export CXX=g++' before running this script |
| 4 | |
| 5 | with_gcc = 0 |
| 6 | is_debug = 1 |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 7 | |
onqtam | 200e888 | 2016-09-20 11:01:02 +0300 | [diff] [blame] | 8 | numFiles = 10 |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 9 | |
onqtam | eae70c7 | 2016-06-07 22:36:24 +0300 | [diff] [blame] | 10 | with_doctest = 1 |
onqtam | 9497821 | 2016-05-22 00:07:30 +0300 | [diff] [blame] | 11 | |
onqtam | eae70c7 | 2016-06-07 22:36:24 +0300 | [diff] [blame] | 12 | with_implement = 1 |
| 13 | with_header = 1 |
onqtam | 200e888 | 2016-09-20 11:01:02 +0300 | [diff] [blame] | 14 | with_num_tests = 50 |
onqtam | df09f51 | 2016-09-15 13:43:50 +0300 | [diff] [blame] | 15 | with_num_assertions = 100 |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 16 | |
onqtam | eae70c7 | 2016-06-07 22:36:24 +0300 | [diff] [blame] | 17 | doctest_disable = 0 |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 18 | doctest_super_fast = 0 |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 19 | |
onqtam | df09f51 | 2016-09-15 13:43:50 +0300 | [diff] [blame] | 20 | the_folder = 'project' |
| 21 | |
onqtam | 7ff1dc1 | 2016-09-18 12:59:51 +0300 | [diff] [blame] | 22 | #macro = "FAST_CHECK_EQ" |
onqtam | 11210ce | 2016-09-19 13:24:23 +0300 | [diff] [blame] | 23 | #macro = "CHECK_EQ" |
onqtam | 7ff1dc1 | 2016-09-18 12:59:51 +0300 | [diff] [blame] | 24 | macro = "CHECK" |
onqtam | 956d4ec | 2016-09-18 13:42:33 +0300 | [diff] [blame] | 25 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 26 | # ============================================================================== |
| 27 | # ============================================================================== |
| 28 | # ============================================================================== |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 29 | |
| 30 | import os |
| 31 | import sys |
| 32 | import random |
| 33 | import string |
| 34 | import multiprocessing |
| 35 | from datetime import datetime |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 36 | import shutil |
| 37 | from time import sleep |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 38 | |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 39 | operator = " == " |
| 40 | if macro != "CHECK": |
| 41 | operator = ", " |
| 42 | |
| 43 | doctest_configs = '' |
| 44 | if doctest_super_fast: |
| 45 | doctest_configs = '#define DOCTEST_CONFIG_SUPER_FAST_ASSERTS' |
| 46 | |
onqtam | 956d4ec | 2016-09-18 13:42:33 +0300 | [diff] [blame] | 47 | GCC_cmake_generator = '"MinGW Makefiles"' |
| 48 | MSVC_cmake_generator = '"Visual Studio 14 Win64"' # MSVC 2015 |
| 49 | |
onqtam | df09f51 | 2016-09-15 13:43:50 +0300 | [diff] [blame] | 50 | if os.name != "nt": |
onqtam | 8337278 | 2016-09-19 23:19:49 +0300 | [diff] [blame] | 51 | GCC_cmake_generator = '"Unix Makefiles"' |
onqtam | df09f51 | 2016-09-15 13:43:50 +0300 | [diff] [blame] | 52 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 53 | # clean and make the folder |
| 54 | if os.path.exists(the_folder): |
| 55 | shutil.rmtree(the_folder) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 56 | |
onqtam | d4669c1 | 2016-05-22 00:24:44 +0300 | [diff] [blame] | 57 | sleep(2) # or the script might fail... |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 58 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 59 | if not os.path.exists(the_folder): |
| 60 | os.makedirs(the_folder) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 61 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 62 | # make the source files |
| 63 | for i in range(0, numFiles): |
| 64 | f = open(the_folder + '/' + str(i) + '.cpp', 'w') |
| 65 | if with_header: |
| 66 | if with_doctest: |
| 67 | if doctest_disable: |
| 68 | f.write('#define DOCTEST_CONFIG_DISABLE\n') |
onqtam | 956d4ec | 2016-09-18 13:42:33 +0300 | [diff] [blame] | 69 | f.write(doctest_configs + '\n') |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 70 | f.write('#include "doctest.h"\n\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 71 | else: |
| 72 | f.write('#include "catch.hpp"\n\n') |
onqtam | eae70c7 | 2016-06-07 22:36:24 +0300 | [diff] [blame] | 73 | for t in range(0, with_num_tests): |
onqtam | 200e888 | 2016-09-20 11:01:02 +0300 | [diff] [blame] | 74 | f.write('TEST_CASE("") {\n') |
| 75 | f.write(' int a = 5;\n int b = 6;\n') |
onqtam | eae70c7 | 2016-06-07 22:36:24 +0300 | [diff] [blame] | 76 | for a in range(0, with_num_assertions): |
onqtam | 200e888 | 2016-09-20 11:01:02 +0300 | [diff] [blame] | 77 | #f.write(' int a' + str(a) + ' = 5;\n') |
| 78 | #f.write(' int b' + str(a) + ' = 6;\n') |
| 79 | #f.write(' ' + macro + '(a' + str(a) + operator + 'b' + str(a) + ');\n') |
onqtam | 19b21e9 | 2016-09-19 23:08:37 +0300 | [diff] [blame] | 80 | f.write(' ' + macro + '(a' + operator + 'b);\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 81 | f.write('}\n\n') |
| 82 | f.write('int f' + str(i) + '() { return ' + str(i) + '; }\n\n') |
| 83 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 84 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 85 | # the main file |
| 86 | f = open(the_folder + '/main.cpp', 'w') |
| 87 | if with_implement: |
| 88 | if with_doctest: |
| 89 | if doctest_disable: |
| 90 | f.write('#define DOCTEST_CONFIG_DISABLE\n') |
onqtam | 956d4ec | 2016-09-18 13:42:33 +0300 | [diff] [blame] | 91 | f.write(doctest_configs + '\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 92 | f.write('#define DOCTEST_CONFIG_IMPLEMENT\n') |
onqtam | c6555cf | 2016-09-20 15:07:28 +0300 | [diff] [blame] | 93 | f.write('#include "doctest.h"\n\n') |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 94 | else: |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 95 | f.write('#define CATCH_CONFIG_RUNNER\n') |
| 96 | f.write('#include "catch.hpp"\n\n') |
| 97 | f.write('int main(int argc, char** argv) {\n') |
| 98 | if with_implement: |
| 99 | if with_doctest: f.write(' int res = doctest::Context(argc, argv).run();\n') |
| 100 | else: f.write(' int res = Catch::Session().run(argc, argv);\n') |
| 101 | else: |
| 102 | f.write(' int res = 0;\n') |
| 103 | for i in range(0, numFiles): |
| 104 | f.write(' int f' + str(i) + '(); res += f' + str(i) + '();\n') |
| 105 | f.write(' return res;\n}\n') |
| 106 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 107 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 108 | # the cmake file |
| 109 | f = open(the_folder + '/CMakeLists.txt', 'w') |
| 110 | f.write('cmake_minimum_required(VERSION 2.8)\n\n') |
onqtam | 9497821 | 2016-05-22 00:07:30 +0300 | [diff] [blame] | 111 | f.write('project(bench)\n\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 112 | if with_doctest: f.write('include_directories("../../../doctest/")\n\n') |
| 113 | else: f.write('include_directories("../catch/single_include/")\n\n') |
onqtam | 9497821 | 2016-05-22 00:07:30 +0300 | [diff] [blame] | 114 | f.write('add_executable(bench main.cpp\n') |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 115 | for i in range(0, numFiles): |
| 116 | f.write(' ' + str(i) + '.cpp\n') |
| 117 | f.write(')\n') |
| 118 | f.close() |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 119 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 120 | # invoke cmake |
| 121 | os.chdir(the_folder); |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 122 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 123 | if with_gcc: |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 124 | cmake_build_type = 'Release' |
| 125 | if is_debug: |
| 126 | cmake_build_type = 'Debug' |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 127 | os.system('cmake . -G ' + GCC_cmake_generator + ' -DCMAKE_BUILD_TYPE=' + cmake_build_type); |
| 128 | else: |
| 129 | os.system('cmake . -G ' + MSVC_cmake_generator); |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 130 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 131 | the_config = '' |
| 132 | if with_gcc == False: |
| 133 | if is_debug: |
| 134 | the_config = ' --config Debug' |
| 135 | else: |
| 136 | the_config = ' --config Release' |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 137 | |
onqtam | d4669c1 | 2016-05-22 00:24:44 +0300 | [diff] [blame] | 138 | # build it |
| 139 | start = datetime.now() |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 140 | os.system('cmake --build .' + the_config) |
| 141 | end = datetime.now() |
onqtam | d4669c1 | 2016-05-22 00:24:44 +0300 | [diff] [blame] | 142 | |
onqtam | 5456545 | 2016-05-19 18:09:20 +0300 | [diff] [blame] | 143 | print("Time for compiling (+ linking): " + str(end - start)) |
hardly | b1e7e14 | 2014-08-06 00:43:51 +0300 | [diff] [blame] | 144 | |
| 145 | os.chdir("../"); |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |