blob: 94ca8c08b62e2a279010ea99848cb52120a063b0 [file] [log] [blame]
Tim Peters84457af2006-03-09 01:59:27 +00001#include <windows.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include <stdio.h>
5
Christian Heimes5b5e81c2007-12-31 16:14:33 +00006#define CMD_SIZE 500
7
Tim Peters84457af2006-03-09 01:59:27 +00008/* 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
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +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
Tim Peters84457af2006-03-09 01:59:27 +000028*/
29
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +000030int make_buildinfo2(const char *tmppath)
Tim Peters84457af2006-03-09 01:59:27 +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;
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +000055 strcat_s(command, CMD_SIZE, "\" .. ..\\Modules\\getbuildinfo.c \"");
56 strcat_s(command, CMD_SIZE, tmppath); /* quoted 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;
Tim Peters84457af2006-03-09 01:59:27 +000062}
63
64int main(int argc, char*argv[])
65{
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +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;
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +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 }
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +000091 if (argc > 2) {
92 tmpdir = argv[2];
93 strcat_s(tmppath, _countof(tmppath), tmpdir);
94 strcat_s(tmppath, _countof(tmppath), "\\");
95 }
Tim Peters84457af2006-03-09 01:59:27 +000096
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +000097 if ((do_unlink = make_buildinfo2(tmppath))) {
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +000098 strcat_s(command, CMD_SIZE, "\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +000099 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000100 strcat_s(command, CMD_SIZE, "getbuildinfo2.c\" -DSUBWCREV ");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000101 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c");
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000103 strcat_s(command, CMD_SIZE, " -Fo\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000104 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000105 strcat_s(command, CMD_SIZE, "getbuildinfo.o\" -I..\\Include -I..\\PC");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 puts(command); fflush(stdout);
107 result = system(command);
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000108 if (do_unlink) {
109 command[0] = '\0';
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000110 strcat_s(command, CMD_SIZE, "\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000111 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000112 strcat_s(command, CMD_SIZE, "getbuildinfo2.c\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000113 _unlink(command);
114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (result < 0)
116 return EXIT_FAILURE;
117 return 0;
118}