Skip to content

Commit 50aae9b

Browse files
authored
Merge pull request #3168 from jeffhostetler/msvc-fixups
MSVC Fixups (redo of headless-git.exe)
2 parents 0489a04 + adbddd3 commit 50aae9b

File tree

7 files changed

+115
-15
lines changed

7 files changed

+115
-15
lines changed

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,12 +2527,12 @@ compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
25272527
compat/nedmalloc/nedmalloc.sp: SP_EXTRA_FLAGS += -Wno-non-pointer-null
25282528
endif
25292529

2530-
headless-git.o: compat/win32/headless.c
2530+
headless-git.o: compat/win32/headless.c GIT-CFLAGS
25312531
$(QUIET_CC)$(CC) $(ALL_CFLAGS) $(COMPAT_CFLAGS) \
25322532
-fno-stack-protector -o $@ -c -Wall -Wwrite-strings $<
25332533

2534-
headless-git$X: headless-git.o git.res
2535-
$(QUIET_LINK)$(CC) $(ALL_LDFLAGS) -mwindows $(COMPAT_CFLAGS) -o $@ $^
2534+
headless-git$X: headless-git.o git.res GIT-LDFLAGS
2535+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -mwindows -o $@ $< git.res
25362536

25372537
git-%$X: %.o GIT-LDFLAGS $(GITLIBS)
25382538
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
@@ -3180,6 +3180,7 @@ cocciclean:
31803180
clean: profile-clean coverage-clean cocciclean
31813181
$(RM) *.res
31823182
$(RM) $(OBJECTS)
3183+
$(RM) headless-git.o
31833184
$(RM) $(LIB_FILE) $(XDIFF_LIB)
31843185
$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
31853186
$(RM) $(TEST_PROGRAMS)
@@ -3208,13 +3209,17 @@ endif
32083209
$(RM) GIT-SCRIPT-DEFINES GIT-PERL-DEFINES GIT-PERL-HEADER GIT-PYTHON-VARS
32093210
ifdef MSVC
32103211
$(RM) $(patsubst %.o,%.o.pdb,$(OBJECTS))
3212+
$(RM) headless-git.o.pdb
32113213
$(RM) $(patsubst %.exe,%.pdb,$(OTHER_PROGRAMS))
3214+
$(RM) $(patsubst %.exe,%.ilk,$(OTHER_PROGRAMS))
32123215
$(RM) $(patsubst %.exe,%.iobj,$(OTHER_PROGRAMS))
32133216
$(RM) $(patsubst %.exe,%.ipdb,$(OTHER_PROGRAMS))
32143217
$(RM) $(patsubst %.exe,%.pdb,$(PROGRAMS))
3218+
$(RM) $(patsubst %.exe,%.ilk,$(PROGRAMS))
32153219
$(RM) $(patsubst %.exe,%.iobj,$(PROGRAMS))
32163220
$(RM) $(patsubst %.exe,%.ipdb,$(PROGRAMS))
32173221
$(RM) $(patsubst %.exe,%.pdb,$(TEST_PROGRAMS))
3222+
$(RM) $(patsubst %.exe,%.ilk,$(TEST_PROGRAMS))
32183223
$(RM) $(patsubst %.exe,%.iobj,$(TEST_PROGRAMS))
32193224
$(RM) $(patsubst %.exe,%.ipdb,$(TEST_PROGRAMS))
32203225
$(RM) compat/vcbuild/MSVC-DEFS-GEN

compat/vcbuild/find_vs_env.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ REM ================================================================
9999

100100
SET sdk_dir=%WindowsSdkDir%
101101
SET sdk_ver=%WindowsSDKVersion%
102+
SET sdk_ver_bin_dir=%WindowsSdkVerBinPath%%tgt%
102103
SET si=%sdk_dir%Include\%sdk_ver%
103104
SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared"
104105
SET sl=%sdk_dir%lib\%sdk_ver%
@@ -130,6 +131,7 @@ REM ================================================================
130131

131132
SET sdk_dir=%WindowsSdkDir%
132133
SET sdk_ver=%WindowsSDKVersion%
134+
SET sdk_ver_bin_dir=%WindowsSdkVerBinPath%bin\amd64
133135
SET si=%sdk_dir%Include\%sdk_ver%
134136
SET sdk_includes=-I"%si%ucrt" -I"%si%um" -I"%si%shared" -I"%si%winrt"
135137
SET sl=%sdk_dir%lib\%sdk_ver%
@@ -160,6 +162,11 @@ REM ================================================================
160162
echo msvc_includes=%msvc_includes%
161163
echo msvc_libs=%msvc_libs%
162164

165+
echo sdk_ver_bin_dir=%sdk_ver_bin_dir%
166+
SET X1=%sdk_ver_bin_dir:C:=/C%
167+
SET X2=%X1:\=/%
168+
echo sdk_ver_bin_dir_msys=%X2%
169+
163170
echo sdk_includes=%sdk_includes%
164171
echo sdk_libs=%sdk_libs%
165172

compat/vcbuild/scripts/clink.pl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
my @lflags = ();
1616
my $is_linking = 0;
1717
my $is_debug = 0;
18+
my $is_gui = 0;
1819
while (@ARGV) {
1920
my $arg = shift @ARGV;
2021
if ("$arg" eq "-DDEBUG") {
@@ -67,7 +68,11 @@
6768
}
6869
push(@args, $lib);
6970
} elsif ("$arg" eq "-lexpat") {
71+
if ($is_debug) {
72+
push(@args, "libexpatd.lib");
73+
} else {
7074
push(@args, "libexpat.lib");
75+
}
7176
} elsif ("$arg" =~ /^-L/ && "$arg" ne "-LTCG") {
7277
$arg =~ s/^-L/-LIBPATH:/;
7378
push(@lflags, $arg);
@@ -119,11 +124,23 @@
119124
push(@cflags, "-wd4996");
120125
} elsif ("$arg" =~ /^-W[a-z]/) {
121126
# let's ignore those
127+
} elsif ("$arg" eq "-fno-stack-protector") {
128+
# eat this
129+
} elsif ("$arg" eq "-mwindows") {
130+
$is_gui = 1;
122131
} else {
123132
push(@args, $arg);
124133
}
125134
}
126135
if ($is_linking) {
136+
if ($is_gui) {
137+
push(@args, "-ENTRY:wWinMainCRTStartup");
138+
push(@args, "-SUBSYSTEM:WINDOWS");
139+
} else {
140+
push(@args, "-ENTRY:wmainCRTStartup");
141+
push(@args, "-SUBSYSTEM:CONSOLE");
142+
}
143+
127144
push(@args, @lflags);
128145
unshift(@args, "link.exe");
129146
} else {

compat/vcbuild/scripts/rc.pl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/perl -w
2+
######################################################################
3+
# Compile Resources on Windows
4+
#
5+
# This is a wrapper to facilitate the compilation of Git with MSVC
6+
# using GNU Make as the build system. So, instead of manipulating the
7+
# Makefile into something nasty, just to support non-space arguments
8+
# etc, we use this wrapper to fix the command line options
9+
#
10+
######################################################################
11+
use strict;
12+
my @args = ();
13+
my @input = ();
14+
15+
while (@ARGV) {
16+
my $arg = shift @ARGV;
17+
if ("$arg" =~ /^-[dD]/) {
18+
# GIT_VERSION gets passed with too many
19+
# layers of dquote escaping.
20+
$arg =~ s/\\"/"/g;
21+
22+
push(@args, $arg);
23+
24+
} elsif ("$arg" eq "-i") {
25+
my $arg = shift @ARGV;
26+
# TODO complain if NULL or is dashed ??
27+
push(@input, $arg);
28+
29+
} elsif ("$arg" eq "-o") {
30+
my $arg = shift @ARGV;
31+
# TODO complain if NULL or is dashed ??
32+
push(@args, "-fo$arg");
33+
34+
} else {
35+
push(@args, $arg);
36+
}
37+
}
38+
39+
push(@args, "-nologo");
40+
push(@args, "-v");
41+
push(@args, @input);
42+
43+
unshift(@args, "rc.exe");
44+
printf("**** @args\n");
45+
46+
exit (system(@args) != 0);

config.mak.uname

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ ifeq ($(uname_S),Windows)
401401
# link.exe next to, and required by, cl.exe, we have to prepend this
402402
# onto the existing $PATH.
403403
#
404-
SANE_TOOL_PATH ?= $(msvc_bin_dir_msys)
404+
SANE_TOOL_PATH ?= $(msvc_bin_dir_msys):$(sdk_ver_bin_dir_msys)
405405
HAVE_ALLOCA_H = YesPlease
406406
NO_PREAD = YesPlease
407407
NEEDS_CRYPTO_WITH_SSL = YesPlease
@@ -456,12 +456,14 @@ endif
456456
compat/win32/trace2_win32_process_info.o \
457457
compat/win32/dirent.o compat/win32/fscache.o
458458
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DENSURE_MSYSTEM_IS_SET -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
459-
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
459+
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO
460460
# invalidcontinue.obj allows Git's source code to close the same file
461461
# handle twice, or to access the osfhandle of an already-closed stdout
462462
# See https://msdn.microsoft.com/en-us/library/ms235330.aspx
463463
EXTLIBS = user32.lib advapi32.lib shell32.lib wininet.lib ws2_32.lib invalidcontinue.obj kernel32.lib ntdll.lib
464+
GITLIBS += git.res
464465
PTHREAD_LIBS =
466+
RC = compat/vcbuild/scripts/rc.pl
465467
lib =
466468
BASIC_CFLAGS += $(vcpkg_inc) $(sdk_includes) $(msvc_includes)
467469
ifndef DEBUG

contrib/buildsystems/Generators/Vcxproj.pm

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ sub createProject {
7676

7777
my $libs_release = "\n ";
7878
my $libs_debug = "\n ";
79-
if (!$static_library) {
79+
if (!$static_library && $name ne 'headless-git') {
8080
$libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
8181
$libs_debug = $libs_release;
8282
$libs_debug =~ s/zlib\.lib/zlibd\.lib/g;
@@ -89,6 +89,16 @@ sub createProject {
8989
$defines =~ s/>/&gt;/g;
9090
$defines =~ s/\'//g;
9191

92+
my $rcdefines = $defines;
93+
$rcdefines =~ s/(?<!\\)"/\\$&/g;
94+
95+
my $entrypoint = 'wmainCRTStartup';
96+
my $subsystem = 'Console';
97+
if (grep /^-mwindows$/, @{$$build_structure{"$prefix${name}_LFLAGS"}}) {
98+
$entrypoint = 'wWinMainCRTStartup';
99+
$subsystem = 'Windows';
100+
}
101+
92102
my $dir = $vcxproj;
93103
$dir =~ s/\/[^\/]*$//;
94104
die "Could not create the directory $dir for $label project!\n" unless (-d "$dir" || mkdir "$dir");
@@ -185,9 +195,9 @@ sub createProject {
185195
<AdditionalLibraryDirectories>\$(VCPKGLibDirectory);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
186196
<AdditionalDependencies>\$(VCPKGLibs);\$(AdditionalDependencies)</AdditionalDependencies>
187197
<AdditionalOptions>invalidcontinue.obj %(AdditionalOptions)</AdditionalOptions>
188-
<EntryPointSymbol>wmainCRTStartup</EntryPointSymbol>
198+
<EntryPointSymbol>$entrypoint</EntryPointSymbol>
189199
<ManifestFile>$cdup\\compat\\win32\\git.manifest</ManifestFile>
190-
<SubSystem>Console</SubSystem>
200+
<SubSystem>$subsystem</SubSystem>
191201
</Link>
192202
EOM
193203
if ($target eq 'libgit') {
@@ -212,6 +222,9 @@ EOM
212222
<PreprocessorDefinitions>WIN32;_DEBUG;$defines;%(PreprocessorDefinitions)</PreprocessorDefinitions>
213223
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
214224
</ClCompile>
225+
<ResourceCompile>
226+
<PreprocessorDefinitions>WIN32;_DEBUG;$rcdefines;%(PreprocessorDefinitions)</PreprocessorDefinitions>
227+
</ResourceCompile>
215228
<Link>
216229
<GenerateDebugInformation>true</GenerateDebugInformation>
217230
</Link>
@@ -225,6 +238,9 @@ EOM
225238
<FunctionLevelLinking>true</FunctionLevelLinking>
226239
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
227240
</ClCompile>
241+
<ResourceCompile>
242+
<PreprocessorDefinitions>WIN32;NDEBUG;$rcdefines;%(PreprocessorDefinitions)</PreprocessorDefinitions>
243+
</ResourceCompile>
228244
<Link>
229245
<GenerateDebugInformation>true</GenerateDebugInformation>
230246
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -234,14 +250,20 @@ EOM
234250
<ItemGroup>
235251
EOM
236252
foreach(@sources) {
237-
print F << "EOM";
253+
if (/\.rc$/) {
254+
print F << "EOM";
255+
<ResourceCompile Include="$_" />
256+
EOM
257+
} else {
258+
print F << "EOM";
238259
<ClCompile Include="$_" />
239260
EOM
261+
}
240262
}
241263
print F << "EOM";
242264
</ItemGroup>
243265
EOM
244-
if (!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') {
266+
if ((!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') && !($name =~ /headless-git/)) {
245267
my $uuid_libgit = $$build_structure{"LIBS_libgit_GUID"};
246268
my $uuid_xdiff_lib = $$build_structure{"LIBS_xdiff/lib_GUID"};
247269

contrib/buildsystems/engine.pl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ sub parseMakeOutput
165165
next;
166166
}
167167

168-
if($text =~ / -c /) {
168+
if($text =~ / -c / || $text =~ / -i \S+\.rc /) {
169169
# compilation
170170
handleCompileLine($text, $line);
171171

@@ -263,16 +263,15 @@ sub handleCompileLine
263263
if ("$part" eq "-o") {
264264
# ignore object file
265265
shift @parts;
266-
} elsif ("$part" eq "-c") {
266+
} elsif ("$part" eq "-c" || "$part" eq "-i" || "$part" =~ /^-fno-/) {
267267
# ignore compile flag
268-
} elsif ("$part" eq "-c") {
269268
} elsif ($part =~ /^.?-I/) {
270269
push(@incpaths, $part);
271270
} elsif ($part =~ /^.?-D/) {
272271
push(@defines, $part);
273272
} elsif ($part =~ /^-/) {
274273
push(@cflags, $part);
275-
} elsif ($part =~ /\.(c|cc|cpp)$/) {
274+
} elsif ($part =~ /\.(c|cc|cpp|rc)$/) {
276275
$sourcefile = $part;
277276
} else {
278277
die "Unhandled compiler option @ line $lineno: $part";
@@ -359,7 +358,7 @@ sub handleLinkLine
359358
push(@libs, $part);
360359
} elsif ($part eq 'invalidcontinue.obj') {
361360
# ignore - known to MSVC
362-
} elsif ($part =~ /\.o$/) {
361+
} elsif ($part =~ /\.(o|res)$/) {
363362
push(@objfiles, $part);
364363
} elsif ($part =~ /\.obj$/) {
365364
# do nothing, 'make' should not be producing .obj, only .o files
@@ -371,7 +370,9 @@ sub handleLinkLine
371370
# exit(1);
372371
foreach (@objfiles) {
373372
my $sourcefile = $_;
373+
$sourcefile =~ s/^headless-git\.o$/compat\/win32\/headless.c/;
374374
$sourcefile =~ s/\.o$/.c/;
375+
$sourcefile =~ s/\.res$/.rc/;
375376
push(@sources, $sourcefile);
376377
push(@cflags, @{$compile_options{"${sourcefile}_CFLAGS"}});
377378
push(@defines, @{$compile_options{"${sourcefile}_DEFINES"}});

0 commit comments

Comments
 (0)