blob: 6190afdb299d9ae1d9da4a51fab95e72ecdca484 [file] [log] [blame]
agsantosac1940d2020-09-17 10:18:40 -04001#!/usr/bin/env python3
2#
agsantos1e7736c2020-10-28 14:39:13 -04003# Copyright (C) 2020 Savoir-faire Linux Inc.
4#
5# Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20# Creates packaging targets for a distribution and architecture.
21# This helps reduce the length of the top Makefile.
22#
23
agsantosac1940d2020-09-17 10:18:40 -040024# This script must unify the Plugins build for
25# every project and Operational System
26
27import os
28import sys
agsantos1e7736c2020-10-28 14:39:13 -040029import json
agsantosac1940d2020-09-17 10:18:40 -040030import platform
31import argparse
32import subprocess
33import multiprocessing
34
35IOS_DISTRIBUTION_NAME = "ios"
36OSX_DISTRIBUTION_NAME = "osx"
37ANDROID_DISTRIBUTION_NAME = "android"
38WIN32_DISTRIBUTION_NAME = "win32"
39UBUNTU_DISTRIBUTION_NAME = "ubuntu"
40
41# vs vars
42win_sdk_default = '10.0.16299.0'
43win_toolset_default = 'v142'
44
45
46def parse():
47 parser = argparse.ArgumentParser(description='Builds Plugins projects')
48 parser.add_argument('--projects', type=str,
49 help='Select plugins to be build.')
50 parser.add_argument('--distribution')
51 parser.add_argument('--processor', type=str, default="GPU",
52 help='Runtime plugin CPU/GPU setting.')
agsantos1e7736c2020-10-28 14:39:13 -040053 parser.add_argument('--buildOptions', default='', type=str,
54 help="Type all build optionsto pass to package.json 'defines' property.\nThis argument consider that you're using cmake.")
agsantosac1940d2020-09-17 10:18:40 -040055
56 dist = choose_distribution()
57
58 if dist == WIN32_DISTRIBUTION_NAME:
59 parser.add_argument('--toolset', default=win_toolset_default, type=str,
60 help='Windows use only, specify Visual Studio toolset version')
61 parser.add_argument('--sdk', default=win_sdk_default, type=str,
62 help='Windows use only, specify Windows SDK version')
63
64 args = parser.parse_args()
65 args.projects = args.projects.split(',')
66 args.processor = args.processor.split(',')
67
68 if (args.distribution is not None):
69 args.distribution = args.distribution.lower()
70 else:
71 args.distribution = dist
72
73 if (len(args.processor) == 1):
74 args.processor *= len(args.projects)
75
76 validate_args(args)
agsantos1e7736c2020-10-28 14:39:13 -040077
78 if dist != WIN32_DISTRIBUTION_NAME:
79 args.toolset = ''
80 args.sdk = ''
agsantosac1940d2020-09-17 10:18:40 -040081 return args
82
83
84def validate_args(parsed_args):
85 """Validate the args values, exit if error is found"""
86
87 # Filter unsupported distributions.
88 supported_distros = [
89 ANDROID_DISTRIBUTION_NAME, UBUNTU_DISTRIBUTION_NAME,
90 WIN32_DISTRIBUTION_NAME
91 ]
92
93 if parsed_args.distribution not in supported_distros:
94 print('Distribution \'{0}\' not supported.\nChoose one of: {1}'.format(
95 parsed_args.distribution, ', '.join(supported_distros)
96 ))
97 sys.exit(1)
98
99 if (len(parsed_args.processor) != len(parsed_args.projects)):
100 sys.exit('Processor must be single or the same size as projects.')
101
102 for processor in parsed_args.processor:
103 if (processor not in ['GPU', 'CPU']):
104 sys.exit('Processor can only be GPU or CPU.')
105
agsantos1e7736c2020-10-28 14:39:13 -0400106 if (parsed_args.buildOptions):
107 parsed_args.buildOptions = parsed_args.buildOptions.split(',')
108
agsantosac1940d2020-09-17 10:18:40 -0400109
110def choose_distribution():
111 system = platform.system().lower()
112
113 if system == "linux" or system == "linux2":
114 if os.path.isfile("/etc/arch-release"):
115 return "arch"
116 with open("/etc/os-release") as f:
117 for line in f:
118 k, v = line.split("=")
119 if k.strip() == 'ID':
120 return v.strip().replace('"', '').split(' ')[0]
121 elif system == "darwin":
122 return OSX_DISTRIBUTION_NAME
123 elif system == "windows":
124 return WIN32_DISTRIBUTION_NAME
125
126 return 'Unknown'
127
128
agsantos1e7736c2020-10-28 14:39:13 -0400129def buildPlugin(pluginPath, processor, distribution, toolset='', sdk='', buildOptions=''):
agsantosac1940d2020-09-17 10:18:40 -0400130 if distribution == WIN32_DISTRIBUTION_NAME:
agsantos1e7736c2020-10-28 14:39:13 -0400131 if (buildOptions):
132 with open(f"{pluginPath}/package.json") as f:
133 defaultPackage = json.load(f)
134 package = defaultPackage.copy()
135 package['defines'] = []
136 for option in buildOptions:
137 package['defines'].append(option)
138 f.close()
139 with open(f"{pluginPath}/package.json", 'w') as f:
140 json.dump(package, f, indent=4)
141 f.close()
142 subprocess.run([
agsantosac1940d2020-09-17 10:18:40 -0400143 sys.executable, os.path.join(
agsantos1e7736c2020-10-28 14:39:13 -0400144 os.getcwd(), "../../daemon/compat/msvc/winmake.py"),
145 "-P",
146 "--toolset", toolset,
147 "--sdk", sdk,
148 "-fb", pluginPath.split('/')[-1]
agsantosac1940d2020-09-17 10:18:40 -0400149 ], check=True)
agsantos1e7736c2020-10-28 14:39:13 -0400150 if (buildOptions):
151 with open(f"{pluginPath}/package.json", "w") as f:
152 json.dump(defaultPackage, f, indent=4)
153 f.close()
154 return
agsantosac1940d2020-09-17 10:18:40 -0400155
156 environ = os.environ.copy()
157
158 install_args = []
159
160 if distribution == ANDROID_DISTRIBUTION_NAME:
161 install_args.append('-t')
162 install_args.append(ANDROID_DISTRIBUTION_NAME)
163 if processor:
164 install_args.append('-c')
165 install_args.append(processor)
166 install_args.append('-p')
167 install_args.append(str(multiprocessing.cpu_count()))
168
agsantos1e7736c2020-10-28 14:39:13 -0400169 subprocess.check_call(['chmod', '+x', pluginPath + "/build.sh"])
agsantosac1940d2020-09-17 10:18:40 -0400170 return subprocess.run([pluginPath + "/build.sh"] +
171 install_args, env=environ, check=True)
172
173
174def main():
175 args = parse()
176 currentDir = os.getcwd()
177
178 for i, plugin in enumerate(args.projects):
179 os.chdir(currentDir + "/" + plugin)
180 buildPlugin(
181 currentDir + "/" + plugin,
182 args.processor[i],
agsantos1e7736c2020-10-28 14:39:13 -0400183 args.distribution,
184 args.toolset,
185 args.sdk,
186 args.buildOptions)
agsantosac1940d2020-09-17 10:18:40 -0400187
188
189if __name__ == "__main__":
190 main()