ยปCore Development>Code coverage>PC/VS9.0/make_buildinfo.c

Python code coverage for PC/VS9.0/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
60n/a puts(command); fflush(stdout);
61n/a if (system(command) < 0)
62n/a return 0;
63n/a return 1;
64n/a}
65n/a
66n/aconst char DELIMS[] = { " \n" };
67n/a
68n/aint get_mercurial_info(char * hgbranch, char * hgtag, char * hgrev, int size)
69n/a{
70n/a int result = 0;
71n/a char filename[CMD_SIZE];
72n/a char cmdline[CMD_SIZE];
73n/a
74n/a strcpy_s(filename, CMD_SIZE, "tmpXXXXXX");
75n/a if (_mktemp_s(filename, CMD_SIZE) == 0) {
76n/a int rc;
77n/a
78n/a strcpy_s(cmdline, CMD_SIZE, "hg id -bit > ");
79n/a strcat_s(cmdline, CMD_SIZE, filename);
80n/a rc = system(cmdline);
81n/a if (rc == 0) {
82n/a FILE * fp;
83n/a
84n/a if (fopen_s(&fp, filename, "r") == 0) {
85n/a char * cp = fgets(cmdline, CMD_SIZE, fp);
86n/a
87n/a if (cp) {
88n/a char * context = NULL;
89n/a char * tp = strtok_s(cp, DELIMS, &context);
90n/a if (tp) {
91n/a strcpy_s(hgrev, size, tp);
92n/a tp = strtok_s(NULL, DELIMS, &context);
93n/a if (tp) {
94n/a strcpy_s(hgbranch, size, tp);
95n/a tp = strtok_s(NULL, DELIMS, &context);
96n/a if (tp) {
97n/a strcpy_s(hgtag, size, tp);
98n/a result = 1;
99n/a }
100n/a }
101n/a }
102n/a }
103n/a fclose(fp);
104n/a }
105n/a }
106n/a _unlink(filename);
107n/a }
108n/a return result;
109n/a}
110n/a
111n/aint main(int argc, char*argv[])
112n/a{
113n/a char command[CMD_SIZE] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
114n/a char tmppath[CMD_SIZE] = "";
115n/a int do_unlink, result;
116n/a char *tmpdir = NULL;
117n/a if (argc <= 2 || argc > 3) {
118n/a fprintf(stderr, "make_buildinfo $(ConfigurationName) [tmpdir]\n");
119n/a return EXIT_FAILURE;
120n/a }
121n/a if (strcmp(argv[1], "Release") == 0) {
122n/a strcat_s(command, CMD_SIZE, "-MD ");
123n/a }
124n/a else if (strcmp(argv[1], "Debug") == 0) {
125n/a strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
126n/a }
127n/a else if (strcmp(argv[1], "ReleaseItanium") == 0) {
128n/a strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
129n/a }
130n/a else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
131n/a strcat_s(command, CMD_SIZE, "-MD ");
132n/a strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
133n/a }
134n/a else {
135n/a fprintf(stderr, "unsupported configuration %s\n", argv[1]);
136n/a return EXIT_FAILURE;
137n/a }
138n/a if (argc > 2) {
139n/a tmpdir = argv[2];
140n/a strcat_s(tmppath, _countof(tmppath), tmpdir);
141n/a /* Hack fix for bad command line: If the command is issued like this:
142n/a * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)"
143n/a * we will get a trailing quote because IntDir ends with a backslash that then
144n/a * escapes the final ". To simplify the life for developers, catch that problem
145n/a * here by cutting it off.
146n/a * The proper command line, btw is:
147n/a * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)\"
148n/a * Hooray for command line parsing on windows.
149n/a */
150n/a if (strlen(tmppath) > 0 && tmppath[strlen(tmppath)-1] == '"')
151n/a tmppath[strlen(tmppath)-1] = '\0';
152n/a strcat_s(tmppath, _countof(tmppath), "\\");
153n/a }
154n/a
155n/a if ((do_unlink = make_buildinfo2(tmppath))) {
156n/a strcat_s(command, CMD_SIZE, "\"");
157n/a strcat_s(command, CMD_SIZE, tmppath);
158n/a strcat_s(command, CMD_SIZE, "getbuildinfo2.c\" -DSUBWCREV ");
159n/a }
160n/a else {
161n/a char hgtag[CMD_SIZE];
162n/a char hgbranch[CMD_SIZE];
163n/a char hgrev[CMD_SIZE];
164n/a
165n/a if (get_mercurial_info(hgbranch, hgtag, hgrev, CMD_SIZE)) {
166n/a strcat_s(command, CMD_SIZE, "-DHGBRANCH=\\\"");
167n/a strcat_s(command, CMD_SIZE, hgbranch);
168n/a strcat_s(command, CMD_SIZE, "\\\"");
169n/a
170n/a strcat_s(command, CMD_SIZE, " -DHGTAG=\\\"");
171n/a strcat_s(command, CMD_SIZE, hgtag);
172n/a strcat_s(command, CMD_SIZE, "\\\"");
173n/a
174n/a strcat_s(command, CMD_SIZE, " -DHGVERSION=\\\"");
175n/a strcat_s(command, CMD_SIZE, hgrev);
176n/a strcat_s(command, CMD_SIZE, "\\\" ");
177n/a }
178n/a strcat_s(command, CMD_SIZE, "..\\..\\Modules\\getbuildinfo.c");
179n/a }
180n/a strcat_s(command, CMD_SIZE, " -Fo\"");
181n/a strcat_s(command, CMD_SIZE, tmppath);
182n/a strcat_s(command, CMD_SIZE, "getbuildinfo.o\" -I..\\..\\Include -I..\\..\\PC");
183n/a puts(command); fflush(stdout);
184n/a result = system(command);
185n/a if (do_unlink) {
186n/a command[0] = '\0';
187n/a strcat_s(command, CMD_SIZE, "\"");
188n/a strcat_s(command, CMD_SIZE, tmppath);
189n/a strcat_s(command, CMD_SIZE, "getbuildinfo2.c\"");
190n/a _unlink(command);
191n/a }
192n/a if (result < 0)
193n/a return EXIT_FAILURE;
194n/a return 0;
195n/a}