summaryrefslogtreecommitdiffstats
path: root/src/tools/rcgen
blob: 0c84772163cd0d092733580e51958bb0a8a28970 (plain)
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
#!/usr/bin/env python3

# Helper for building resource files when building for windows. Always
# generates a .rc from the input .rc file. When building with msvc we
# additionally generate a .res file with 'rc', when building with gcc, we use
# windres to directly generate a .o.  Additionally we generate basic
# dependencies if depfile is specified.

import argparse
import os
import subprocess
import sys

parser = argparse.ArgumentParser(description='generate PostgreSQL rc file')

parser.add_argument('--srcdir', type=os.path.abspath,
                    required=True)
parser.add_argument('--builddir', type=os.path.abspath,
                    required=True)

binaries = parser.add_argument_group('binaries')
binaries.add_argument('--windres', type=os.path.abspath)
binaries.add_argument('--rc', type=os.path.abspath)

inout = parser.add_argument_group('inout')
inout.add_argument('--depfile', type=argparse.FileType('w'))
inout.add_argument('--input', type=argparse.FileType('r'),
                   required=True)
inout.add_argument('--rcout', type=argparse.FileType('w'),
                   required=True)
inout.add_argument('--out', type=str,
                   required=True)

replacements = parser.add_argument_group('replacements')
replacements.add_argument('--FILEDESC', type=str)
replacements.add_argument('--NAME', type=str, required=True)
replacements.add_argument('--VFT_TYPE', type=str, required=True)
replacements.add_argument('--FILEENDING', type=str, required=True)
replacements.add_argument('--ICO', type=str)

args = parser.parse_args()

# determine replacement strings

internal_name = '"{0}"'.format(args.NAME)
original_name = '"{0}.{1}"'.format(args.NAME, args.FILEENDING)

# if no description is passed in, generate one based on the name
if args.FILEDESC:
    filedesc = args.FILEDESC
elif args.NAME:
    if args.VFT_TYPE == 'VFT_DLL':
        filedesc = 'PostgreSQL {0} library'.format(args.NAME)
    else:
        filedesc = 'PostgreSQL {0} binary'.format(args.NAME)
filedesc = '"{0}"'.format(filedesc)


if args.ICO:
    ico = 'IDI_ICON ICON "{0}"'.format(args.ICO)
    if args.depfile:
        args.depfile.write("{0} : {1}\n".format(args.rcout.name, args.ICO))
else:
    ico = ''


data = args.input.read()

data = data.replace('VFT_APP', args.VFT_TYPE)
data = data.replace('_INTERNAL_NAME_', internal_name)
data = data.replace('_ORIGINAL_NAME_', original_name)
data = data.replace('FILEDESC', filedesc)
data = data.replace("_ICO_", ico)

args.rcout.write(data)
args.rcout.close()

if args.windres:
    cmd = [
        args.windres,
        '-I{0}/src/include/'.format(args.builddir),
        '-I{0}/src/include/'.format(args.srcdir),
        '-o', args.out, '-i', args.rcout.name,
    ]
elif args.rc:
    cmd = [
        args.rc, '/nologo',
        '-I{0}/src/include/'.format(args.builddir),
        '-I{0}/src/include/'.format(args.srcdir),
        '/fo', args.out, args.rcout.name,
    ]
else:
    sys.exit('either --windres or --rc needs to be specified')

sp = subprocess.run(cmd)
if sp.returncode != 0:
    sys.exit(sp.returncode)

# It'd be nicer if we could generate correct dependencies here, but 'rc'
# doesn't support doing so. It's unlikely we'll ever need more, so...
if args.depfile:
    args.depfile.write("{0} : {1}\n".format(
        args.rcout.name, args.input.name))
    args.depfile.write("{0} : {1}/{2}\n".format(
        args.out, args.builddir, 'src/include/pg_config.h'))