forked from Kvixen/Simulix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
97 lines (81 loc) · 3.73 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Simulix generates an FMU from a simulink model source code.
Copyright (C) 2018 Scania CV AB and Simulix contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from os import mkdir, path, getcwd, chdir
import argparse
import subprocess
from shutil import which
from platform import system
BUILD_ONLY = False
def execute_build_commands(compile_program,make_program,dst, exe_cmake, exe_make, debug_build):
if exe_cmake:
command = "cmake -G {0} -DCMAKE_BUILD_TYPE={1} ..".format(compile_program, "Release" if not debug_build else "Debug")
print("Executing {0}".format(command))
cmake_p = subprocess.Popen(command, shell=True)
cmake_p.communicate() # Wait for program to finish
if exe_make:
command = "{0}".format(make_program)
print("Executing {0}".format(command))
make_p = subprocess.Popen(command, shell=True)
make_p.communicate() # Wait for program to finish
return
def prepare_build_directory(dst, folder_name):
#If theres no build folder or we're currently in the build folder
if not path.isdir(path.join(dst, folder_name)) and dst.split('/')[-1:] != folder_name:
mkdir(path.join(dst, folder_name))
chdir(path.join(dst, folder_name))
#If theres a build folder but we're currently not in it
elif dst.split('/')[-1:] != folder_name:
chdir(path.join(dst, folder_name))
def find_generate_prog(x):
return {
'mingw32-make' : "\"MinGW Makefiles\"",
'nmake' : "\"NMake Makefiles\"",
'make' : "\"Unix Makefiles\"",
'ninja' : "\"Ninja\""
}[x]
def find_make_prog():
if("Windows" in system()):
if(which("NMAKE")):
return ("\"NMake Makefiles\"", "NMAKE")
elif(which("mingw32-make")):
return ("\"MinGW Makefiles\"", "mingw32-make")
elif("Linux" in system()):
if(which("make")):
return ("\"Unix Makefiles\"", "make")
elif(which("Ninja")):
return ("\"Ninja\"", "Ninja")
def main(dst, folder_name, make_prog, exe_cmake, exe_make, debug_build):
if not path.isabs(dst):
dst = path.join(getcwd(), dst)
if not path.isdir(dst):
exit("Path specified doesn't exist")
if not make_prog or not which(make_prog):
make_program = find_make_prog()
elif which(make_prog):
make_program = (find_generate_prog(make_prog.lower()), make_prog)
if not make_program:
exit("Couldn't find Make program")
print("Building")
prepare_build_directory(dst, folder_name)
execute_build_commands(make_program[0], make_program[1], dst, exe_cmake, exe_make, debug_build)
chdir("..")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Executes CMake with Makefile generator",prog="build",usage="%(prog)s [options]")
parser.add_argument('p', nargs='?', help='Path to files for building', default=getcwd())
parser.add_argument('-m', help='Makefile program')
parser.add_argument('--DEBUG-BUILD', help='Build as debug', action='store_true')
parser.add_argument('-f', help='Build folder name', default='build')
args = parser.parse_args()
main(args.p, args.f, args.m, True, True, args.debug_build)