blob: 8efa07a36495254baedc969a97e93bec9ceaddfb [file] [log] [blame]
Christian Heimes57dddfb2008-01-02 18:30:52 +00001#include <windows.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <stdio.h>
5
6#define CMD_SIZE 500
7
8/* This file creates the getbuildinfo.o object, by first
9 invoking subwcrev.exe (if found), and then invoking cl.exe.
10 As a side effect, it might generate PCBuild\getbuildinfo2.c
11 also. If this isn't a subversion checkout, or subwcrev isn't
12 found, it compiles ..\\..\\Modules\\getbuildinfo.c instead.
13
14 Currently, subwcrev.exe is found from the registry entries
15 of TortoiseSVN.
16
17 No attempt is made to place getbuildinfo.o into the proper
18 binary directory. This isn't necessary, as this tool is
19 invoked as a pre-link step for pythoncore, so that overwrites
20 any previous getbuildinfo.o.
21
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000022 However, if a second argument is provided, this will be used
23 as a temporary directory where any getbuildinfo2.c and
24 getbuildinfo.o files are put. This is useful if multiple
25 configurations are being built in parallel, to avoid them
26 trampling each other's files.
27
Christian Heimes57dddfb2008-01-02 18:30:52 +000028*/
29
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000030int make_buildinfo2(const char *tmppath)
Christian Heimes57dddfb2008-01-02 18:30:52 +000031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 struct _stat st;
33 HKEY hTortoise;
34 char command[CMD_SIZE+1];
35 DWORD type, size;
36 if (_stat(".svn", &st) < 0)
37 return 0;
38 /* Allow suppression of subwcrev.exe invocation if a no_subwcrev file is present. */
39 if (_stat("no_subwcrev", &st) == 0)
40 return 0;
41 if (RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS &&
42 RegOpenKey(HKEY_CURRENT_USER, "Software\\TortoiseSVN", &hTortoise) != ERROR_SUCCESS)
43 /* Tortoise not installed */
44 return 0;
45 command[0] = '"'; /* quote the path to the executable */
46 size = sizeof(command) - 1;
47 if (RegQueryValueEx(hTortoise, "Directory", 0, &type, command+1, &size) != ERROR_SUCCESS ||
48 type != REG_SZ)
49 /* Registry corrupted */
50 return 0;
51 strcat_s(command, CMD_SIZE, "bin\\subwcrev.exe");
52 if (_stat(command+1, &st) < 0)
53 /* subwcrev.exe not part of the release */
54 return 0;
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000055 strcat_s(command, CMD_SIZE, "\" ..\\.. ..\\..\\Modules\\getbuildinfo.c ");
56 strcat_s(command, CMD_SIZE, tmppath);
57 strcat_s(command, CMD_SIZE, "getbuildinfo2.c");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 puts(command); fflush(stdout);
59 if (system(command) < 0)
60 return 0;
61 return 1;
Christian Heimes57dddfb2008-01-02 18:30:52 +000062}
63
64int main(int argc, char*argv[])
65{
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000066 char command[CMD_SIZE] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL ";
67 char tmppath[CMD_SIZE] = "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 int do_unlink, result;
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000069 char *tmpdir = NULL;
70 if (argc <= 2 || argc > 3) {
71 fprintf(stderr, "make_buildinfo $(ConfigurationName) [tmpdir]\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 return EXIT_FAILURE;
73 }
74 if (strcmp(argv[1], "Release") == 0) {
75 strcat_s(command, CMD_SIZE, "-MD ");
76 }
77 else if (strcmp(argv[1], "Debug") == 0) {
78 strcat_s(command, CMD_SIZE, "-D_DEBUG -MDd ");
79 }
80 else if (strcmp(argv[1], "ReleaseItanium") == 0) {
81 strcat_s(command, CMD_SIZE, "-MD /USECL:MS_ITANIUM ");
82 }
83 else if (strcmp(argv[1], "ReleaseAMD64") == 0) {
84 strcat_s(command, CMD_SIZE, "-MD ");
85 strcat_s(command, CMD_SIZE, "-MD /USECL:MS_OPTERON ");
86 }
87 else {
88 fprintf(stderr, "unsupported configuration %s\n", argv[1]);
89 return EXIT_FAILURE;
90 }
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000091 if (argc > 2) {
92 tmpdir = argv[2];
93 strcat_s(tmppath, _countof(tmppath), tmpdir);
94 strcat_s(tmppath, _countof(tmppath), "\\");
95 }
Christian Heimes57dddfb2008-01-02 18:30:52 +000096
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +000097 if ((do_unlink = make_buildinfo2(tmppath))) {
98 strcat_s(command, CMD_SIZE, tmppath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 strcat_s(command, CMD_SIZE, "getbuildinfo2.c -DSUBWCREV ");
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +0000100 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 strcat_s(command, CMD_SIZE, "..\\..\\Modules\\getbuildinfo.c");
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +0000102 strcat_s(command, CMD_SIZE, " -Fo");
103 strcat_s(command, CMD_SIZE, tmppath);
104 strcat_s(command, CMD_SIZE, "getbuildinfo.o -I..\\..\\Include -I..\\..\\PC");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 puts(command); fflush(stdout);
106 result = system(command);
Hirokazu Yamamoto5ee89cf2010-11-04 15:21:59 +0000107 if (do_unlink) {
108 command[0] = '\0';
109 strcat_s(command, CMD_SIZE, tmppath);
110 strcat_s(command, CMD_SIZE, "getbuildinfo2.c");
111 _unlink(command);
112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (result < 0)
114 return EXIT_FAILURE;
115 return 0;
116}