blob: f85cf3337f2f6a4b33970bb51865654d593017b5 [file] [log] [blame]
agsantosac1940d2020-09-17 10:18:40 -04001#!/usr/bin/env python3
2#
Sébastien Blincb783e32021-02-12 11:34:10 -05003# Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantos1e7736c2020-10-28 14:39:13 -04004#
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
agsantosac1940d2020-09-17 10:18:40 -040041def parse():
42 parser = argparse.ArgumentParser(description='Builds Plugins projects')
43 parser.add_argument('--projects', type=str,
44 help='Select plugins to be build.')
45 parser.add_argument('--distribution')
46 parser.add_argument('--processor', type=str, default="GPU",
47 help='Runtime plugin CPU/GPU setting.')
agsantos1e7736c2020-10-28 14:39:13 -040048 parser.add_argument('--buildOptions', default='', type=str,
49 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 -040050
51 dist = choose_distribution()
52
agsantosac1940d2020-09-17 10:18:40 -040053
54 args = parser.parse_args()
55 args.projects = args.projects.split(',')
56 args.processor = args.processor.split(',')
57
58 if (args.distribution is not None):
59 args.distribution = args.distribution.lower()
60 else:
61 args.distribution = dist
62
63 if (len(args.processor) == 1):
64 args.processor *= len(args.projects)
65
66 validate_args(args)
agsantos1e7736c2020-10-28 14:39:13 -040067
agsantosac1940d2020-09-17 10:18:40 -040068 return args
69
70
71def validate_args(parsed_args):
72 """Validate the args values, exit if error is found"""
73
74 # Filter unsupported distributions.
75 supported_distros = [
76 ANDROID_DISTRIBUTION_NAME, UBUNTU_DISTRIBUTION_NAME,
agsantos1bbc7cc2021-05-20 16:43:35 -040077 WIN32_DISTRIBUTION_NAME, OSX_DISTRIBUTION_NAME
agsantosac1940d2020-09-17 10:18:40 -040078 ]
79
80 if parsed_args.distribution not in supported_distros:
81 print('Distribution \'{0}\' not supported.\nChoose one of: {1}'.format(
82 parsed_args.distribution, ', '.join(supported_distros)
83 ))
84 sys.exit(1)
85
86 if (len(parsed_args.processor) != len(parsed_args.projects)):
87 sys.exit('Processor must be single or the same size as projects.')
88
89 for processor in parsed_args.processor:
90 if (processor not in ['GPU', 'CPU']):
91 sys.exit('Processor can only be GPU or CPU.')
92
agsantos1e7736c2020-10-28 14:39:13 -040093 if (parsed_args.buildOptions):
94 parsed_args.buildOptions = parsed_args.buildOptions.split(',')
95
agsantosac1940d2020-09-17 10:18:40 -040096
97def choose_distribution():
98 system = platform.system().lower()
99
100 if system == "linux" or system == "linux2":
101 if os.path.isfile("/etc/arch-release"):
102 return "arch"
103 with open("/etc/os-release") as f:
104 for line in f:
105 k, v = line.split("=")
106 if k.strip() == 'ID':
107 return v.strip().replace('"', '').split(' ')[0]
108 elif system == "darwin":
109 return OSX_DISTRIBUTION_NAME
110 elif system == "windows":
111 return WIN32_DISTRIBUTION_NAME
112
113 return 'Unknown'
114
115
Aline Gondim Santosd251ea62023-03-02 09:40:56 -0300116def buildPlugin(pluginPath, processor, distribution, buildOptions=''):
agsantosac1940d2020-09-17 10:18:40 -0400117 if distribution == WIN32_DISTRIBUTION_NAME:
agsantos1e7736c2020-10-28 14:39:13 -0400118 if (buildOptions):
119 with open(f"{pluginPath}/package.json") as f:
120 defaultPackage = json.load(f)
121 package = defaultPackage.copy()
122 package['defines'] = []
123 for option in buildOptions:
124 package['defines'].append(option)
125 f.close()
126 with open(f"{pluginPath}/package.json", 'w') as f:
127 json.dump(package, f, indent=4)
128 f.close()
129 subprocess.run([
agsantosac1940d2020-09-17 10:18:40 -0400130 sys.executable, os.path.join(
agsantos1e7736c2020-10-28 14:39:13 -0400131 os.getcwd(), "../../daemon/compat/msvc/winmake.py"),
132 "-P",
agsantos1e7736c2020-10-28 14:39:13 -0400133 "-fb", pluginPath.split('/')[-1]
agsantosac1940d2020-09-17 10:18:40 -0400134 ], check=True)
agsantos1e7736c2020-10-28 14:39:13 -0400135 if (buildOptions):
136 with open(f"{pluginPath}/package.json", "w") as f:
137 json.dump(defaultPackage, f, indent=4)
138 f.close()
139 return
agsantosac1940d2020-09-17 10:18:40 -0400140
141 environ = os.environ.copy()
142
143 install_args = []
144
145 if distribution == ANDROID_DISTRIBUTION_NAME:
146 install_args.append('-t')
147 install_args.append(ANDROID_DISTRIBUTION_NAME)
148 if processor:
149 install_args.append('-c')
150 install_args.append(processor)
151 install_args.append('-p')
152 install_args.append(str(multiprocessing.cpu_count()))
153
agsantos1e7736c2020-10-28 14:39:13 -0400154 subprocess.check_call(['chmod', '+x', pluginPath + "/build.sh"])
agsantosac1940d2020-09-17 10:18:40 -0400155 return subprocess.run([pluginPath + "/build.sh"] +
156 install_args, env=environ, check=True)
157
158
159def main():
160 args = parse()
161 currentDir = os.getcwd()
162
163 for i, plugin in enumerate(args.projects):
164 os.chdir(currentDir + "/" + plugin)
165 buildPlugin(
166 currentDir + "/" + plugin,
167 args.processor[i],
agsantos1e7736c2020-10-28 14:39:13 -0400168 args.distribution,
agsantos1e7736c2020-10-28 14:39:13 -0400169 args.buildOptions)
agsantosac1940d2020-09-17 10:18:40 -0400170
171
172if __name__ == "__main__":
173 main()