-
Notifications
You must be signed in to change notification settings - Fork 16
/
build_libs.py
186 lines (146 loc) · 5.13 KB
/
build_libs.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env/python3
# Copyright 2021 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# See BUILD.gn in this directory for an explanation of what this script is for.
import argparse
import os
import stat
import sys
import shutil
import subprocess
import re
def main():
parser = argparse.ArgumentParser("build_libs.py")
parser.add_argument("--clang-base-dir",
help="Path to Clang binaries",
required=False),
parser.add_argument("--output",
help="Path to library output",
required=True)
parser.add_argument("--depfile", help="Path to write depfile", required=False)
args = parser.parse_args()
run_build(args)
# Generate dep file
if not args.depfile:
return
# root_project/src/beacon/core
beacon_libs_dir = os.path.dirname(os.path.realpath(__file__))
# Ninja depfile Format:
# <output path to lib>: <path to each Go file>
with open(args.depfile, 'w') as depfile:
depfile.write("%s:" % args.output)
for root, dirs, files in os.walk(beacon_libs_dir):
for f in files:
if f.endswith('.go'):
infile = os.path.join(root, f)
depfile.write(" %s" % infile)
depfile.write("\n")
def create_build_env(clang_base_dir):
env = os.environ.copy()
if not clang_base_dir or sys.platform == 'win32':
return env
clang_bin_dir = os.path.join(clang_base_dir, "bin")
if "PATH" in env:
env["PATH"] = clang_bin_dir + ":" + env["PATH"]
else:
env["PATH"] = clang_bin_dir
env["CC"] = os.path.join(clang_bin_dir, "clang")
return env
# Removes dylibs so darwin target doesn't link
# against them favoring static libs
def remove_dylibs(d):
if sys.platform != 'darwin':
return
l = os.listdir(d)
for item in l:
if item.endswith(".dylib"):
os.remove(os.path.join(d, item))
def must_exist(p):
if not os.path.exists(p):
raise Exception("path " + p + " doesn't exist")
def to_msys_path(p):
if os.name != 'nt':
return p
return p.replace("C:", "/c").replace("\\", "/")
def call(command, env):
# On Windows, we need to compile hnsd using MSYS64
if sys.platform == 'win32':
env["MSYSTEM"] = "MINGW64"
cwd = '"{0}"'.format(to_msys_path(os.getcwd()))
command = [r"C:\msys64\usr\bin\bash", "-l" , "-c", "cd " + cwd + " && " + ' '.join(command)]
try:
subprocess.check_call(command,env=env)
except subprocess.CalledProcessError as e:
print(e.output)
raise e
# Builds this project must be called after build_hsk
def build_core(out, env):
goBin = 'go'
# We use MSYS on windows to compile CGO code
# Note: TDM GCC may still be needed as a depedency.
if sys.platform == 'win32':
goBin = '"/c/Program Files/Go/bin/go.exe"'
out = to_msys_path(out)
call_args = [goBin,'build','-trimpath','-buildmode=c-shared', '-o', out]
if sys.platform == 'win32':
call(call_args, env)
return
# keep in sync with the same min mac osx version used
# by chromium
if sys.platform == 'darwin':
env['CGO_CFLAGS'] = '-mmacosx-version-min=10.11.0'
env['CGO_LDFLAGS'] = '-mmacos-version-min=10.11.0'
try:
subprocess.check_call(call_args, env=env)
except subprocess.CalledProcessError as e:
print(e.output)
raise e
# call install tool to update loader path
# this way it can be found by chromium app bundle
# we currently dlopen the lib anyways so this
# isn't necessary but my still be useful.
if sys.platform == 'darwin':
call(['install_name_tool','-id', '@loader_path/Libraries/libbeacon.dylib', out], env)
# Builds libhsk must be called from hnsd root directory
def build_hsk(hsk_out_path, env):
# start a clean build
try:
call(['make', 'clean'], env)
except Exception:
pass
call(['./autogen.sh'], env)
hsk_out_path = to_msys_path(hsk_out_path)
call(['./configure', '--without-daemon', '--prefix', hsk_out_path],env)
if sys.platform == 'darwin':
call(['make', 'CFLAGS=-mmacosx-version-min=10.11.0', '-j'], env)
else:
call(['make', '-j'], env)
# installs to hsk_out_path
call(['make', 'install'], env)
# remove dylibs
remove_dylibs(os.path.join(hsk_out_path, "lib"))
def run_build(args):
env = create_build_env(args.clang_base_dir)
# root_project/src/beacon/core
#+root_project/src/beacon/components/core
this_dir = os.path.dirname(os.path.realpath(__file__))
# root_project/src/beacon
beacon_dir = os.path.dirname(os.path.dirname(this_dir))
third_party_dir = os.path.join(beacon_dir, "third_party")
libhsk_src = os.path.join(beacon_dir, "third_party", "hnsd")
hsq_src = os.path.join(beacon_dir, "third_party", "hnsquery")
libhsk_out = os.path.join(hsq_src, "build")
# Sanity check
must_exist(hsq_src)
must_exist(libhsk_src)
# Starting libhsk build
# don't build if lib already exists
if not os.path.exists(os.path.join(libhsk_out, "lib")):
os.chdir(libhsk_src)
build_hsk(libhsk_out, env)
# Building this project
os.chdir(this_dir)
build_core(args.output, env)
if __name__ == '__main__':
sys.exit(main())