ยปCore Development>Code coverage>PCbuild/make_buildinfo.c

Python code coverage for PCbuild/make_buildinfo.c

#countcontent
1n/a#include <windows.h>
2n/a#include <sys/types.h>
3n/a#include <sys/stat.h>
4n/a#include <stdio.h>
5n/a#include <io.h>
6n/a
7n/a#define CMD_SIZE 500
8n/a
9n/a/* This file creates the getbuildinfo.o object, by first
10n/a invoking subwcrev.exe (if found), and then invoking cl.exe.
11n/a As a side effect, it might generate PCBuild\getbuildinfo2.c
12n/a also. If this isn't a subversion checkout, or subwcrev isn't
13n/a found, it compiles ..\\Modules\\getbuildinfo.c instead.
14n/a
15n/a Currently, subwcrev.exe is found from the registry entries
16n/a of TortoiseSVN.
17n/a
18n/a No attempt is made to place getbuildinfo.o into the proper
19n/a binary directory. This isn't necessary, as this tool is
20n/a invoked as a pre-link step for pythoncore, so that overwrites
21n/a any previous getbuildinfo.o.
22n/a
23n/a However, if a second argument is provided, this will be used
24n/a as a temporary directory where any getbuildinfo2.c and
25n/a getbuildinfo.o files are put. This is useful if multiple
26n/a configurations are being built in parallel, to avoid them
27n/a trampling each other's files.
28n/a
29n/a*/
30n/a
31n/aint make_buildinfo2(const char *tmppath)
32n/a{
33n/a struct _stat st;
34n/a HKEY hTortoise;
35n/a char command[CMD_SIZE+1];
36n/a DWORD type, size;
37n/a if (_stat(".svn", &st) < 0)
38n/a return 0;
39n/a /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
40n/a if (_stat("no_subwcrev", &st) == 0)
41n/a return 0;
42n/a if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
43n/a RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
44n/a /* Tortoise not installed */
45n/a return 0;
46n/a command[0] = '"'; /* quote the path to the executable */
47n/a size = sizeof(command) - 1;
48n/a if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
49n/a type != REG_SZ)
50n/a /* Registry corrupted */
51n/a return 0;
52n/a strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
53n/a if (_stat(command+1, &st) < 0)
54n/a /* subwcrev.exe not part of the release */
55n/a return 0;
56n/a strcat_s(command, CMD_SIZE, "\" .. ..\\Modules\\getbuildinfo.c \"");
57n/a strcat_s(command, CMD_SIZE, tmppath); /* quoted tmppath */
58n/a strcat_s(command, CMD_SIZE, "getbuildinfo2.c\"");
59n/a puts(command); fflush(stdout);
60n/a if (system(command) < 0)
61n/a return 0;
62n/a return 1;
63n/a}
64n/a
65n/aconst char DELIMS[] = { " \n" };
66n/a
67n/aint get_mercurial_info(char * hgbranch, char * hgtag, char * hgrev, int size)
68n/a{
69n/a int result = 0;
70n/a char filename[CMD_SIZE];
71n/a char cmdline[CMD_SIZE];
72n/a
73n/a strcpy_s(filename, CMD_SIZE, "tmpXXXXXX");
74n/a if (_mktemp_s(filename, CMD_SIZE) == 0) {
75n/a int rc;
76n/a
77n/a strcpy_s(cmdline, CMD_SIZE, "hg id -bit > ");
78n/a strcat_s(cmdline, CMD_SIZE, filename);
79n/a rc = system(cmdline);
80n/a if (rc == 0) {
81n/a FILE * fp;
82n/a
83n/a if (fopen_s(&fp, filename, "r") == 0) {
84n/a char * cp = fgets(cmdline, CMD_SIZE, fp);
85n/a
86n/a if (cp) {
87n/a char * context = NULL;
88n/a char * tp = strtok_s(cp, DELIMS, &context);
89n/a if (tp) {
90n/a strcpy_s(hgrev, size, tp);
91n/a tp = strtok_s(NULL, DELIMS, &context);
92n/a if (tp) {
93n/a strcpy_s(hgbranch, size, tp);
94n/a tp = strtok_s(NULL, DELIMS, &context);
95n/a if (tp) {
96n/a strcpy_s(hgtag, size, tp);
97n/a result = 1;
98n/a }
99n/a }
100n/a }
101n/a }
102n/a fclose(fp);
103n/a }
104n/a }
105n/a _unlink(filename);
106n/a }
107n/a return result;
108n/a}
109n/a
110n/aint main(int argc, char*argv[])
111n/a{
112n/a char command[CMD_SIZE] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
113n/a char tmppath[CMD_SIZE] = "";
114n/a int do_unlink, result;
115n/a char *tmpdir = NULL;
116n/a if (argc <= 2 || argc > 3) {
117n/a fprintf(stderr, "make_buildinfo $(ConfigurationName) [tmpdir]\n");
118n/a return EXIT_FAILURE;
119n/a }
120n/a if (strcmp(argv[1], "Release") == 0) {
121n/a strcat_s(command, CMD_SIZE, "-MD ");
122n/a }
123n/a else if (strcmp(argv[1], "Debug") == 0) {
124n/a strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
125n/a }
126n/a else if (strcmp(argv[1], "ReleaseItanium") == 0) {
127n/a strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
128n/a }
129n/a else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
130n/a strcat_s(command, CMD_SIZE, "-MD ");
131n/a strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
132n/a }
133n/a else {
134n/a fprintf(stderr, "unsupported configuration %s\n", argv[1]);
135n/a return EXIT_FAILURE;
136n/a }
137n/a if (argc > 2) {
138n/a tmpdir = argv[2];
139n/a strcat_s(tmppath, _countof(tmppath), tmpdir);
140n/a /* Hack fix for bad command line: If the command is issued like this:
141n/a * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)"
142n/a * we will get a trailing quote because IntDir ends with a backslash that then
143n/a * escapes the final ". To simplify the life for developers, catch that problem
144n/a * here by cutting it off.
145n/a * The proper command line, btw is:
146n/a * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)\"
147n/a * Hooray for command line parsing on windows.
148n/a */
149n/a if (strlen(tmppath) > 0 && tmppath[strlen(tmppath)-1] == '"')
150n/a tmppath[strlen(tmppath)-1] = '\0';
151n/a strcat_s(tmppath, _countof(tmppath), "\\");
152n/a }
153n/a
154n/a if ((do_unlink = make_buildinfo2(tmppath))) {
155n/a strcat_s(command, CMD_SIZE, "\"");
156n/a strcat_s(command, CMD_SIZE, tmppath);
157n/a strcat_s(command, CMD_SIZE, "getbuildinfo2.c\" -DSUBWCREV ");
158n/a }
159n/a else {
160n/a char hgtag[CMD_SIZE];
161n/a char hgbranch[CMD_SIZE];
162n/a char hgrev[CMD_SIZE];
163n/a
164n/a if (get_mercurial_info(hgbranch, hgtag, hgrev, CMD_SIZE)) {
165n/a strcat_s(command, CMD_SIZE, "-DHGBRANCH=\\\"");
166n/a strcat_s(command, CMD_SIZE, hgbranch);
167n/a strcat_s(command, CMD_SIZE, "\\\"");
168n/a
169n/a strcat_s(command, CMD_SIZE, " -DHGTAG=\\\"");
170n/a strcat_s(command, CMD_SIZE, hgtag);
171n/a strcat_s(command, CMD_SIZE, "\\\"");
172n/a
173n/a strcat_s(command, CMD_SIZE, " -DHGVERSION=\\\"");
174n/a strcat_s(command, CMD_SIZE, hgrev);
175n/a strcat_s(command, CMD_SIZE, "\\\" ");
176n/a }
177n/a strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c");
178n/a }
179n/a strcat_s(command, CMD_SIZE, " -Fo\"");
180n/a strcat_s(command, CMD_SIZE, tmppath);
181n/a strcat_s(command, CMD_SIZE, "getbuildinfo.o\" -I..\\Include -I..\\PC");
182n/a puts(command); fflush(stdout);
183n/a result = system(command);
184n/a if (do_unlink) {
185n/a command[0] = '\0';
186n/a strcat_s(command, CMD_SIZE, "\"");
187n/a strcat_s(command, CMD_SIZE, tmppath);
188n/a strcat_s(command, CMD_SIZE, "getbuildinfo2.c\"");
189n/a _unlink(command);
190n/a }
191n/a if (result < 0)
192n/a return EXIT_FAILURE;
193n/a return 0;
194n/a}