Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 1 | #include <windows.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/stat.h> |
| 4 | #include <stdio.h> |
| 5 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 6 | #define CMD_SIZE 500 |
| 7 | |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 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 | |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 22 | 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 Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 30 | int make_buildinfo2(const char *tmppath) |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 31 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | 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ónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 55 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | puts(command); fflush(stdout); |
| 59 | if (system(command) < 0) |
| 60 | return 0; |
| 61 | return 1; |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int main(int argc, char*argv[]) |
| 65 | { |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 66 | char command[CMD_SIZE] = "cl.exe -c -D_WIN32 -DUSE_DL_EXPORT -D_WINDOWS -DWIN32 -D_WINDLL "; |
| 67 | char tmppath[CMD_SIZE] = ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | int do_unlink, result; |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 69 | char *tmpdir = NULL; |
| 70 | if (argc <= 2 || argc > 3) { |
| 71 | fprintf(stderr, "make_buildinfo $(ConfigurationName) [tmpdir]\n"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | 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ónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 91 | if (argc > 2) { |
| 92 | tmpdir = argv[2]; |
| 93 | strcat_s(tmppath, _countof(tmppath), tmpdir); |
Kristján Valur Jónsson | 8d28a92 | 2010-12-13 03:32:10 +0000 | [diff] [blame] | 94 | /* 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ónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 105 | strcat_s(tmppath, _countof(tmppath), "\\"); |
| 106 | } |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 107 | |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 108 | if ((do_unlink = make_buildinfo2(tmppath))) { |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 109 | strcat_s(command, CMD_SIZE, "\""); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 110 | strcat_s(command, CMD_SIZE, tmppath); |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 111 | strcat_s(command, CMD_SIZE, "getbuildinfo2.c\" -DSUBWCREV "); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 112 | } else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | strcat_s(command, CMD_SIZE, "..\\Modules\\getbuildinfo.c"); |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 114 | strcat_s(command, CMD_SIZE, " -Fo\""); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 115 | strcat_s(command, CMD_SIZE, tmppath); |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 116 | strcat_s(command, CMD_SIZE, "getbuildinfo.o\" -I..\\Include -I..\\PC"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | puts(command); fflush(stdout); |
| 118 | result = system(command); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 119 | if (do_unlink) { |
| 120 | command[0] = '\0'; |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 121 | strcat_s(command, CMD_SIZE, "\""); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 122 | strcat_s(command, CMD_SIZE, tmppath); |
Kristján Valur Jónsson | 60fafa2 | 2010-11-22 11:37:06 +0000 | [diff] [blame] | 123 | strcat_s(command, CMD_SIZE, "getbuildinfo2.c\""); |
Kristján Valur Jónsson | 33d144a | 2010-11-03 13:57:00 +0000 | [diff] [blame] | 124 | _unlink(command); |
| 125 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | if (result < 0) |
| 127 | return EXIT_FAILURE; |
| 128 | return 0; |
| 129 | } |