|
1 |
| -#!/usr/bin/env python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | import os
|
| 5 | +import sys |
5 | 6 | from shutil import Error, copy2, rmtree
|
6 | 7 | import subprocess
|
7 | 8 | from collections import defaultdict
|
|
34 | 35 | "enumRelOpts",
|
35 | 36 | "stringRelOpts"])
|
36 | 37 |
|
| 38 | +source_dirs = [ |
| 39 | + "postgresql/src/backend", |
| 40 | + "postgresql/src/common", |
| 41 | + "postgresql/src/include", |
| 42 | + "postgresql/src/port", |
| 43 | + "postgresql/src/timezone", |
| 44 | +] |
| 45 | + |
| 46 | +def is_inside_source_dirs(filename): |
| 47 | + for dir in source_dirs: |
| 48 | + if filename.startswith(dir): |
| 49 | + return True |
| 50 | + return False |
| 51 | + |
| 52 | +no_copy_sources = [ |
| 53 | + # not used (and linux/darwin-only) |
| 54 | + "postgresql/src/backend/port/posix_sema.c", |
| 55 | + "postgresql/src/backend/port/sysv_shmem.c", |
| 56 | + # not used, was excluded earlier |
| 57 | + "postgresql/src/include/utils/help_config.h", |
| 58 | + "postgresql/src/backend/utils/misc/help_config.c", |
| 59 | + # provided in libc_compat |
| 60 | + "postgresql/src/port/strlcat.c", |
| 61 | + "postgresql/src/port/strlcpy.c", |
| 62 | + |
| 63 | + # unneded headers |
| 64 | + "postgresql/src/common/md5_int.h", |
| 65 | + "postgresql/src/common/sha1_int.h", |
| 66 | + "postgresql/src/common/sha2_int.h", |
| 67 | + "postgresql/src/include/common/connect.h", |
| 68 | + "postgresql/src/include/common/logging.h", |
| 69 | + "postgresql/src/include/common/restricted_token.h", |
| 70 | + "postgresql/src/include/fe_utils/", |
| 71 | + "postgresql/src/include/getopt_long.h", |
| 72 | + "postgresql/src/include/jit/llvmjit.h", |
| 73 | + "postgresql/src/include/jit/llvmjit_emit.h", |
| 74 | + "postgresql/src/include/libpq/be-gssapi-common.h", |
| 75 | + "postgresql/src/include/port/aix.h", |
| 76 | + "postgresql/src/include/port/atomics/arch-hppa.h", |
| 77 | + "postgresql/src/include/port/atomics/arch-ia64.h", |
| 78 | + "postgresql/src/include/port/atomics/arch-ppc.h", |
| 79 | + "postgresql/src/include/port/atomics/generic-acc.h", |
| 80 | + "postgresql/src/include/port/atomics/generic-sunpro.h", |
| 81 | + "postgresql/src/include/port/cygwin.h", |
| 82 | + "postgresql/src/include/port/darwin.h", |
| 83 | + "postgresql/src/include/port/freebsd.h", |
| 84 | + "postgresql/src/include/port/hpux.h", |
| 85 | + "postgresql/src/include/port/linux.h", |
| 86 | + "postgresql/src/include/port/netbsd.h", |
| 87 | + "postgresql/src/include/port/openbsd.h", |
| 88 | + "postgresql/src/include/port/pg_pthread.h", |
| 89 | + "postgresql/src/include/port/solaris.h", |
| 90 | + "postgresql/src/include/port/win32/dlfcn.h", |
| 91 | + "postgresql/src/include/port/win32.h", |
| 92 | + "postgresql/src/include/replication/pgoutput.h", |
| 93 | + "postgresql/src/include/snowball/", |
| 94 | + "postgresql/src/port/pthread-win32.h", |
| 95 | +] |
| 96 | + |
| 97 | +def need_copy(filename): |
| 98 | + if not is_inside_source_dirs(filename): |
| 99 | + return False |
| 100 | + for prefix in no_copy_sources: |
| 101 | + if filename.startswith(prefix): |
| 102 | + return False |
| 103 | + return True |
| 104 | + |
| 105 | +exclude_from_source_list = set([ |
| 106 | + # platform-specific, explicitly added in ya.make |
| 107 | + "postgresql/src/port/pg_crc32c_sse42.c", |
| 108 | + "postgresql/src/port/pg_crc32c_sse42_choose.c", |
| 109 | +]) |
| 110 | + |
37 | 111 | def fix_line(line, all_lines, pos):
|
38 | 112 | global inside_func
|
39 | 113 | global define_for_yylval
|
@@ -218,45 +292,52 @@ def mycopy2(src, dst):
|
218 | 292 | if line is not None:
|
219 | 293 | fdst.write(line)
|
220 | 294 |
|
221 |
| -def copytree(src, dst): |
222 |
| - names = os.listdir(src) |
223 |
| - os.makedirs(dst) |
| 295 | +def copy_and_patch_sources(src_dir): |
224 | 296 | errors = []
|
225 |
| - for name in names: |
226 |
| - if name == "help_config.c": continue |
227 |
| - srcname = os.path.join(src, name) |
228 |
| - dstname = os.path.join(dst, name) |
229 |
| - #print(srcname) |
230 |
| - try: |
231 |
| - if os.path.isdir(srcname): |
232 |
| - copytree(srcname, dstname) |
| 297 | + with open(os.path.join(src_dir, "src_files"), "r") as fd: |
| 298 | + for line in fd: |
| 299 | + name = line.strip() |
| 300 | + if not need_copy(name): |
| 301 | + continue |
| 302 | + srcname = os.path.join(src_dir, name) |
| 303 | + if name == "postgresql/src/include/pg_config.h": |
| 304 | + dstname = "postgresql/src/include/pg_config-linux.h" |
233 | 305 | else:
|
234 |
| - mycopy2(srcname, dstname) |
235 |
| - except OSError as why: |
236 |
| - errors.append((srcname, dstname, str(why))) |
237 |
| - except Error as err: |
238 |
| - errors.extend(err.args[0]) |
239 |
| - if errors: |
240 |
| - raise Error(errors) |
241 |
| - |
242 |
| -def make_sources_list(): |
243 |
| - with open("../../../../../contrib/libs/postgresql/ya.make","r") as fsrc: |
244 |
| - with open("pg_sources.inc","w") as fdst: |
245 |
| - fdst.write("SRCS(\n") |
246 |
| - for line in fsrc: |
247 |
| - if line.startswith(" src/"): |
248 |
| - #print(line.strip()) |
249 |
| - if "/help_config.c" in line: continue |
250 |
| - fdst.write(" postgresql/" + line.strip() + "\n") |
251 |
| - fdst.write(")\n") |
252 |
| - |
253 |
| -def get_vars(): |
254 |
| - s=subprocess.check_output("objdump ../../../../../contrib/libs/postgresql/libpostgres.a -tw",shell=True).decode("utf-8") |
| 306 | + dstname = name |
| 307 | + try: |
| 308 | + os.makedirs(os.path.dirname(dstname), mode=0o755, exist_ok=True) |
| 309 | + if os.path.islink(srcname): |
| 310 | + target_full = os.path.realpath(srcname) |
| 311 | + target = os.path.relpath(target_full, start=os.path.realpath(os.path.dirname(srcname))) |
| 312 | + with open(dstname, "w") as f: |
| 313 | + print('#include "' + target + '" /* inclink generated by yamaker */', file=f) |
| 314 | + else: |
| 315 | + mycopy2(srcname, dstname) |
| 316 | + except OSError as why: |
| 317 | + errors.append((srcname, dstname, str(why))) |
| 318 | + except Error as err: |
| 319 | + errors.extend(err.args[0]) |
| 320 | + if errors: |
| 321 | + raise Error(errors) |
| 322 | + |
| 323 | +def make_sources_list(build_dir): |
| 324 | + with open(f"{build_dir}/src_files","r") as fsrc: |
| 325 | + with open("pg_sources.inc","w") as fdst: |
| 326 | + fdst.write("SRCS(\n") |
| 327 | + for line in fsrc: |
| 328 | + #print(line.strip()) |
| 329 | + name = line.strip() |
| 330 | + if name.endswith(".c") and need_copy(name) and name not in exclude_from_source_list: |
| 331 | + fdst.write(" " + name + "\n") |
| 332 | + fdst.write(")\n") |
| 333 | + |
| 334 | +def get_vars(build_dir): |
| 335 | + s=subprocess.check_output(f"objdump {build_dir}/postgresql/src/backend/postgres.a -tw",shell=True).decode("utf-8") |
255 | 336 | for a in s.replace("\t"," ").split("\n"):
|
256 | 337 | for b in a.split(" "):
|
257 | 338 | sym=None
|
258 | 339 | if b.startswith(".bss."): sym=b[5:]
|
259 |
| - elif b.startswith(".data."): sym=b[6:] |
| 340 | + elif b.startswith(".data.") and not b.startswith(".data.rel.ro."): sym=b[6:] |
260 | 341 | if sym is not None:
|
261 | 342 | all_vars.add(sym.replace("yql_",""))
|
262 | 343 |
|
@@ -300,23 +381,25 @@ def write_thread_inits():
|
300 | 381 |
|
301 | 382 | void pg_thread_init(void) {
|
302 | 383 | if (pg_thread_init_flag) return;
|
303 |
| - pg_thread_init_flag=1; |
304 |
| - setup_pg_thread_cleanup(); |
305 |
| - pg_timezone_initialize(); |
306 |
| -""", file=f) |
| 384 | + pg_thread_init_flag=1;""", file=f) |
307 | 385 |
|
308 | 386 | for a in sorted(thread_funcs):
|
309 | 387 | print(" " + a + "();", file=f)
|
310 |
| - print("}", file=f) |
| 388 | + print(""" |
| 389 | + setup_pg_thread_cleanup(); |
| 390 | + pg_timezone_initialize(); |
| 391 | +}""", file=f) |
311 | 392 |
|
312 | 393 | with open("thread_inits.h","w") as f:
|
313 | 394 | print("#pragma once", file=f)
|
314 | 395 | print("extern void pg_thread_init();", file=f)
|
315 | 396 |
|
316 | 397 | if __name__ == "__main__":
|
317 |
| - get_vars() |
318 |
| - make_sources_list() |
319 |
| - if os.path.isdir("postgresql"): |
320 |
| - rmtree("postgresql") |
321 |
| - copytree("../../../../../contrib/libs/postgresql", "postgresql") |
| 398 | + if len(sys.argv) != 2: |
| 399 | + print("Usage ", sys.argv[0], " <build directory>"); |
| 400 | + sys.exit(1) |
| 401 | + build_dir=sys.argv[1] |
| 402 | + get_vars(build_dir) |
| 403 | + make_sources_list(build_dir) |
| 404 | + copy_and_patch_sources(build_dir) |
322 | 405 | write_thread_inits()
|
0 commit comments