blob: fd3a2e2f6b36d09f6df40d75499840792f66f889 [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);
Kristján Valur Jónsson8d28a922010-12-13 03:32:10 +000094 /* Hack fix for bad command line: If the command is issued like this:
95 * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)"
96 * we will get a trailing quote because IntDir ends with a backslash that then
97 * escapes the final ". To simplify the life for developers, catch that problem
98 * here by cutting it off.
99 * The proper command line, btw is:
100 * $(SolutionDir)make_buildinfo.exe" Debug "$(IntDir)\"
101 * Hooray for command line parsing on windows.
102 */
103 if (strlen(tmppath) > 0 && tmppath[strlen(tmppath)-1] == '"')
104 tmppath[strlen(tmppath)-1] = '\0';
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000105 strcat_s(tmppath, _countof(tmppath), "\\");
106 }
Tim Peters84457af2006-03-09 01:59:27 +0000107
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000108 if ((do_unlink = make_buildinfo2(tmppath))) {
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000109 strcat_s(command, CMD_SIZE, "\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000110 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000111 strcat_s(command, CMD_SIZE, "getbuildinfo2.c\" -DSUBWCREV ");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000112 } else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c");
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000114 strcat_s(command, CMD_SIZE, " -Fo\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000115 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000116 strcat_s(command, CMD_SIZE, "getbuildinfo.o\" -I..\\Include -I..\\PC");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 puts(command); fflush(stdout);
118 result = system(command);
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000119 if (do_unlink) {
120 command[0] = '\0';
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000121 strcat_s(command, CMD_SIZE, "\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000122 strcat_s(command, CMD_SIZE, tmppath);
Kristján Valur Jónsson60fafa22010-11-22 11:37:06 +0000123 strcat_s(command, CMD_SIZE, "getbuildinfo2.c\"");
Kristján Valur Jónsson33d144a2010-11-03 13:57:00 +0000124 _unlink(command);
125 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (result < 0)
127 return EXIT_FAILURE;
128 return 0;
129}