Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows support #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
PREFIX=/usr/local
TEST_CFLAGS=-D_QRINTF_COUNT_CALL=1 -Wall -g -Werror
AOUT=./test
ifeq (Windows_NT, $(OS))
#AOUT=.\\\test.exe
AOUT=".\test.exe"
endif

all:

Expand All @@ -15,9 +20,9 @@ install:
test: test-cc test-cxx

test-cc:
bin/qrintf $(CC) $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o ./test && ./test
perl bin/qrintf $(CC) $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o $(AOUT) && $(AOUT)

test-cxx:
bin/qrintf $(CXX) -x c++ $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o ./test && ./test
perl bin/qrintf $(CXX) -x c++ $(TEST_CFLAGS) t/test.c deps/picotest/picotest.c -o $(AOUT) && $(AOUT)

.PHONY: gen install test test-cc test-cxx
17 changes: 10 additions & 7 deletions bin/qrintf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use warnings;
use Digest::SHA qw(sha1_hex);
use File::Basename qw(basename dirname);
use File::Temp qw(tempdir);
use POSIX qw(WIFEXITED WEXITSTATUS WTERMSIG);
use File::Spec::Functions qw(rel2abs);

my %EXT = (
c => 'i',
Expand All @@ -42,14 +42,16 @@ if (@ARGV && $ARGV[0] eq '--version') {
exit 0;
}

my $pwd = dirname($0);
my $pwd = dirname(rel2abs($0));

my $cc = shift @ARGV;

if (basename($cc) =~ m{^g(?:cc|\+\+)}) {
# is GCC, use "-wrapper"
exec "$cc", qw(-no-integrated-cpp -wrapper), "$pwd/../share/qrintf/gcc-wrapper", @ARGV;
die "failed to exec $cc:$!";
if (system("$cc", qw(-no-integrated-cpp -wrapper), "$pwd/../share/qrintf/gcc-wrapper", @ARGV) != 0) {
die "failed to exec $cc:$!";
}
exit 0;
} else {
# mimic GCC's "-no-integrated-cpp -wrapper"

Expand Down Expand Up @@ -87,6 +89,7 @@ if (basename($cc) =~ m{^g(?:cc|\+\+)}) {
# preprocess the source files
for my $fn (@files) {
run_cmd(
$^X,
"$pwd/../share/qrintf/gcc-wrapper",
$cc, '-E',
(grep { $_ ne '-c' } @ARGV),
Expand All @@ -112,10 +115,10 @@ sub run_cmd {
system(@argv) == 0 and return;
if ($? == -1) {
die "failed to exec $argv[0]:$!";
} elsif (WIFEXITED($?)) {
exit WEXITSTATUS($?);
} elsif ($? & 255) {
exit $? >> 8;
} else {
die "$argv[0] exitted due to signal @{[WTERMSIG($?)]}\n";
die "$argv[0] exitted due to signal @{[$? & 255]}\n";
}
}

Expand Down
2 changes: 1 addition & 1 deletion bin/qrintf-pp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use File::Basename qw(dirname);

if (@ARGV && $ARGV[0] eq '--version') {
my $cmd = dirname($0) . "/qrintf";
exec $cmd, "--version";
exec $^X, $cmd, "--version";
die "failed to execute: $cmd:$!";
}

Expand Down
6 changes: 4 additions & 2 deletions include/qrintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ extern "C" {

#undef sprintf
#define sprintf(...) _qp_sprintf(__VA_ARGS__)
#undef snprintf
#define snprintf(...) _qp_snprintf(__VA_ARGS__)
#ifndef _WIN32
# undef snprintf
# define snprintf(...) _qp_snprintf(__VA_ARGS__)
#endif

#if _QRINTF_COUNT_CALL
extern size_t _qrintf_call_cnt;
Expand Down
12 changes: 7 additions & 5 deletions share/qrintf/gcc-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ use File::Basename qw(dirname);
use POSIX qw(WIFEXITED WEXITSTATUS);

if (! grep { $_ eq '-E' } @ARGV) {
exec @ARGV;
die "failed to exec $ARGV[0]:$!";
if (system(@ARGV) != 0) {
die "failed to exec $ARGV[0]:$!";
}
exit 0;
}

my $pwd = dirname($0);

# invoke cpp
my ($cpp_cmd, @cpp_args) = @ARGV;
system($cpp_cmd, '-DQRINTF=1', '-include', "$pwd/../../include/qrintf.h", @cpp_args) == 0
or exit(WIFEXITED($?) ? WEXITSTATUS($?) : 1);
or exit($? >> 8 ? $? >> 8 : 1);

# read the source file
my $src_fn = $ARGV[$#ARGV];
Expand All @@ -45,8 +47,8 @@ my $src = do {
};

# filter and print
open my $fh, "| $pwd/../../bin/qrintf-pp > $src_fn"
open my $fh, "| $^X $pwd/../../bin/qrintf-pp > $src_fn"
or die "failed to invoke $pwd/qrintf-pp:$!";
print $fh $src;
close $fh
or exit(WIFEXITED($?) ? WEXITSTATUS($?) : 1);
or exit($? >> 8 ? $? >> 8 : 1);
8 changes: 8 additions & 0 deletions t/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,25 @@ static void test_simple(void)
CHECK_MULTI(unsigned , "%u", 0, UINT_MAX);
CHECK_MULTI(unsigned long, "%lu", 0, ULONG_MAX);
CHECK_MULTI(unsigned long long, "%llu", 0, ULLONG_MAX);
#ifndef _WIN32
CHECK_MULTI(size_t, "%zu", 0, SIZE_MAX);
#endif

CHECK_MULTI(unsigned short, "%hx", 0, USHRT_MAX);
CHECK_MULTI(unsigned , "%x", 0, UINT_MAX);
CHECK_MULTI(unsigned long, "%lx", 0, ULONG_MAX);
CHECK_MULTI(unsigned long long, "%llx", 0, ULLONG_MAX);
#ifndef _WIN32
CHECK_MULTI(size_t, "%zx", 0, SIZE_MAX);
#endif

CHECK_MULTI(unsigned short, "%hX", 0, USHRT_MAX);
CHECK_MULTI(unsigned , "%X", 0, UINT_MAX);
CHECK_MULTI(unsigned long, "%lX", 0, ULONG_MAX);
CHECK_MULTI(unsigned long long, "%llX", 0, ULLONG_MAX);
#ifndef _WIN32
CHECK_MULTI(size_t, "%zX", 0, SIZE_MAX);
#endif

CHECK_MULTI(int, "%7d", INT_MIN, INT_MAX);
CHECK_MULTI(int, "%07d", INT_MIN, INT_MAX);
Expand All @@ -133,6 +139,7 @@ static void test_simple(void)
CHECK_MULTI(unsigned, "%7x", 0, UINT_MAX);
CHECK_MULTI(unsigned, "%07x", 0, UINT_MAX);

#ifndef _WIN32
CHECK_SNPRINTF(8, "%s", "abcdef"); /* below the bounds */
CHECK_SNPRINTF(8, "1234%s", "abcdef"); /* partial write */
CHECK_SNPRINTF(8, "1234567890%s", "abcdef"); /* no write */
Expand All @@ -145,6 +152,7 @@ static void test_simple(void)
CHECK_SNPRINTF(3, "%x", UINT_MAX);
CHECK_SNPRINTF(3, "%lx", ULONG_MAX);
CHECK_SNPRINTF(3, "%llx", ULLONG_MAX);
#endif
}

static void test_composite(void)
Expand Down