Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[6model/c] patches for OS X (bbkr++ for server shell account) and MinGW
  • Loading branch information
mberends committed Aug 8, 2011
1 parent ed14a43 commit 2bed502
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 65 deletions.
1 change: 1 addition & 0 deletions c/t/01-toolchain/01a-cc.c
Expand Up @@ -287,6 +287,7 @@ remove_lib()
int
main(int argc, char * argv[])
{
printf("# 01a-cc\n");
printf("1..16\n"); /* tests */
create_exe(); /* 1-2 make testexe.c and testexe.exe */
run_exe(); /* 2-5 run testexe.exe */
Expand Down
5 changes: 3 additions & 2 deletions c/t/01-toolchain/01b-timing.c
Expand Up @@ -53,19 +53,20 @@ seconds_microseconds_sleep()
seconds2, microseconds2);
ok(seconds2 > seconds1 || microseconds2 >= microseconds1, message);

/* test 3 - time3 is approximately time2 + 1000000 microseconds */
/* test 3 - time3 is within 2ms of time2 + 1 second */
timediff = (seconds3 - seconds2) * 1000000
+ microseconds3 - microseconds2;
sprintf(message, "one second sleep measured %lld microseconds",
timediff);
ok(999000 < timediff && timediff < 1000999, message);
ok(998000 < timediff && timediff < 1002000, message);
}


/* main */
int
main(int arg, char * argv[])
{
diag("01b-timing");
plan(3);
seconds_microseconds_sleep();
return 0;
Expand Down
13 changes: 8 additions & 5 deletions c/t/01-toolchain/01c-threads.c
@@ -1,4 +1,4 @@
/* 01b-threads.c */
/* 01c-threads.c */
/* Check that threading in the operating system and libraries are ok */


Expand Down Expand Up @@ -38,7 +38,7 @@ void
tests1_4()
{
int status;
#ifdef _MSC_VER
#ifdef _WIN32
HANDLE threadhandle1, threadhandle2;
DWORD threadId1, threadId2;
#else
Expand All @@ -53,7 +53,7 @@ tests1_4()
thread1arguments.seconds = 2;
thread1arguments.description = "first";
threadstacksize = 16384; /* Minimum allowed by Posix threads */
#ifdef _MSC_VER
#ifdef _WIN32
threadhandle1 = CreateThread(NULL, threadstacksize,
(LPTHREAD_START_ROUTINE) test1_thread, &thread1arguments, 0,
&threadId1);
Expand All @@ -71,7 +71,7 @@ tests1_4()
thread2arguments.testnumber = 4;
thread2arguments.seconds = 2;
thread2arguments.description = "second";
#ifdef _MSC_VER
#ifdef _WIN32
threadhandle2 = CreateThread(NULL, threadstacksize,
(LPTHREAD_START_ROUTINE) test1_thread, &thread2arguments, 0,
&threadId1);
Expand All @@ -81,7 +81,7 @@ tests1_4()
&thread2arguments);
#endif
is_ii(status, 0, "created second thread");
#ifndef _MSC_VER
#ifndef _WIN32
pthread_attr_destroy(&thread_attr);
#endif

Expand All @@ -94,7 +94,10 @@ tests1_4()
int
main(int arg, char * argv[])
{
diag("01c-threads");
plan(4);
tests1_4(); /* two simple child threads */
return 0;
}

/* end of 01c-threads.c */
3 changes: 2 additions & 1 deletion c/t/02-components/02a-hashtable.c
Expand Up @@ -11,7 +11,7 @@
#include <string.h> /* memmove strlen */
#include <sys/time.h> /* gettimeofday */
#include "../../src/hashtable.h"
#include "../Test.h" /* is plan */
#include "../Test.h" /* diag is plan */

/* The number of allocations is O((STRINGCOUNT ** 2) * MAXKEYLENGTH), */
/* so be careful when increasing it. 5000 strings use 250MiB. */
Expand Down Expand Up @@ -99,6 +99,7 @@ int main(int argc, char *argv[])
value_bytes = 0, entrynumber, collisions = 0, delete_count;
char * source, * destination;

diag("02a-hashtable");
plan(4);
gettimeofday(&time_now, NULL);
time_write.tv_sec = time_now.tv_sec + 5;
Expand Down
128 changes: 108 additions & 20 deletions c/tools/build/Configure.c
Expand Up @@ -21,14 +21,16 @@
* http://www.thefreecountry.com/compilers/cpp.shtml
* Most of them alias cc to their own filenames.
* MinGW
* http://mingw.org/
* Currently based on GCC 4.5.2, 85MB disk.
* Targets Win32 libraries, no Posix emulation or dlopen.
* (older version bundled with Git full install)
* msysGit
* The full install is a 39MB download instead of the 13MB Git install, but it expands to 1.3GB instead of
* about 200MB, and includes MinGW, which includes GCC.
* MinGW
* http://www.mingw.org/
* Also bundled with the Git full install. It includes bash, so use Configure.sh rather than Configure.bat
* The libraries do not include dlopen.
* It includes bash, so use Configure.sh rather than Configure.bat
* lcc-win32
* http://www.cs.virginia.edu/~lcc-win32/
Expand Down Expand Up @@ -56,12 +58,23 @@
#include <stdio.h> /* fclose fgets FILE fopen fprintf printf stderr */
#include <stdlib.h> /* exit free getenv malloc realloc */
#include <string.h> /* memmove memcpy strcpy strlen strstr */

#if __APPLE__
#include <sys/sysctl.h>
#elif __linux__
#include <unistd.h> /* sysconf */
#elif _WIN32
#include <windows.h> /* GetSystemInfo */
#endif

#define LINEBUFFERSIZE 128
/* Subscript names for scanned evidence. Almost like a perl hash ;) */
enum { OS_VAR,
DETECTED_END /* This one must always be last */};
char * detected[DETECTED_END] = {""};
/* Subscript names for configuration strings. Almost like a hash ;) */
enum { CC, EXE, LDL, MAKE_COMMAND, OS_TYPE, OUT, RM_RF,
enum { CC, EXE, LDL, MAKE_COMMAND, OSTYPE, OUTFILE, RM_RF,
CONFIG_END /* this one must always be last */ };
/* note the words OS_TYPE and OUT clash with MinGW */
char * config[CONFIG_END] = {"", "", "", "", "", "", ""};
/* forward references to internal functions */
void detect(void);
Expand All @@ -70,39 +83,112 @@ void squirt(char * text, char * filename);
void trans(char ** text, char * search, char * replace);


/* detection */
/* Find and show differences between compilers and operating systems */
void
detection()
{
printf("Configure detects the following:\n");

/* Operating system */
printf(" Operating system in C predefined macro: ");
#if __APPLE__
printf("__APPLE__");
#endif
#if __linux__
printf("__linux__");
#endif
#if _WIN32
printf("_WIN32");
#endif
#if !(__APPLE__ | __linux__ | _WIN32)
printf("unknown\n (not __APPLE__ __linux__ or _WIN32)\n");
#endif
printf("\n");

/* C compiler */
printf(" C compiler in predefined macro: ");
#if __GNUC__
printf("__GNUC__");
#endif
#if _MSC_VER
printf("_MSC_VER");
#endif
#if !(__GNUC__ | _MSC_VER)
printf("unknown\n (not __GNUC__ or _MSC_VER)")
#endif
printf("\n");

/* Number of processors */
int processors = 0;
/* from http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine */
#if __APPLE__
int mib[4] = {CTL_HW, HW_NCPU, 0, 0};
size_t size = sizeof(processors);
sysctl(mib, 2, &processors, &size, NULL, 0);
#elif linux
processors = sysconf(_SC_NPROCESSORS_ONLN);
#elif _WIN32
SYSTEM_INFO sysinfo; GetSystemInfo( &sysinfo );
processors = sysinfo.dwNumberOfProcessors;
#endif
printf(" Number of processors: %d\n", processors);

/* Sizes of built in data types */
printf("Data sizes: char short int long long_long float double pointer\n");
printf(" bytes: %3d %3d %3d %3d %3d %3d %3d %3d\n",
(int)sizeof(char), (int)sizeof(short), (int)sizeof(int),
(int)sizeof(long), (int)sizeof(long long), (int)sizeof(float),
(int)sizeof(double), (int)sizeof(void *) );
printf(" bits: %3d %3d %3d %3d %3d %3d %3d %3d\n",
(int)sizeof(char)*8, (int)sizeof(short)*8, (int)sizeof(int)*8,
(int)sizeof(long)*8, (int)sizeof(long long)*8, (int)sizeof(float)*8,
(int)sizeof(double)*8, (int)sizeof(void *)*8);
}

/* config_set */
/* Use environment variables and other clues to assign values to */
/* members of the config[] array */

void
config_set(void)
{
char * s;
/* Operating system */ s=getenv("OS");
/* Operating system */
s=getenv("OS");
if (s && strcmp(s,"Windows_NT")==0) { /* any Windows system */
config[OS_TYPE] = "Windows";
config[OSTYPE] = "Windows";
config[EXE] = ".exe";
}
else { /* non Windows operating systems default to Unix settings */
config[OS_TYPE] = "Unix";
config[OSTYPE] = "Unix";
config[EXE] = "";
}
/* C compiler */ s=getenv("COMPILER");
if (s && strcmp(s,"MSVC")==0) {
/* TODO: use _MSC_VER */
/* See http://msdn.microsoft.com/en-US/library/b0084kay%28v=VS.100%29.aspx */
config[CC] = "cl -DMSVC ";
config[OUT] = "-Fe";
config[RM_RF] = "del /F /Q /S";
config[OUTFILE] = "-Fe";
}
if (s && strcmp(s,"GCC")==0) { s=getenv("OS");
if (s && strcmp(s,"GCC")==0) {
s=getenv("OS");
if (s && strcmp(s,"Windows_NT")==0)
config[CC] = "cc -DGCC ";
config[CC] = "gcc -DGCC ";
else
config[CC] = "cc -DGCC -ldl ";
config[OUT] = "-o";
config[RM_RF] = "rm -rf";
config[OUTFILE] = "-o";
}

/* File delete command */
config[RM_RF] =
#if __unix__
"rm -rf";
#if _WIN32
"del /F /Q /S";
#else
"error: no file delete command";
#endif

/* Make utility */ s=getenv("COMPILER");
if (s && strcmp(s,"GCC")==0) {
config[MAKE_COMMAND] = "make";
Expand All @@ -124,15 +210,16 @@ makefile_convert(char * programfilename, char * templatefilename,
trans(&makefiletext, "This is the file", "This is NOT the file");
trans(&makefiletext, "@cc@", config[CC]);
trans(&makefiletext, "@exe@", config[EXE]);
trans(&makefiletext, "@out@", config[OUT]);
trans(&makefiletext, "@outfile@", config[OUTFILE]);
trans(&makefiletext, "@rm_rf@", config[RM_RF]);
if (strcmp(config[OS_TYPE], "Windows")==0 && strcmp(getenv("COMPILER"),"MSVC")==0) {
#if _WIN32
trans(&makefiletext, "src/", "src\\");
trans(&makefiletext, "tools/build/", "tools\\build\\");
trans(&makefiletext, "t/01-toolchain/", "t\\01-toolchain\\");
trans(&makefiletext, "t/01-toolchain", "t\\01-toolchain");
trans(&makefiletext, "t/02-components/", "t\\02-components\\");
trans(&makefiletext, "t/02-components", "t\\02-components");
}
#endif
printf(" %s: writing to %s\n", programfilename, outputfilename);
squirt(makefiletext, outputfilename);
free(makefiletext);
Expand Down Expand Up @@ -239,6 +326,7 @@ main(int argc, char * argv[])
fprintf(stderr, "Usage: %s path/to/Makefile.in path/to/Makefile\n", argv[0]);
exit(1);
}
detection();
config_set(); /* Figure out the configuration settings */
makefile_convert(argv[0], argv[1], argv[2]);
printf("Use '%s' to build and test 6model/c\n", config[MAKE_COMMAND]);
Expand Down
31 changes: 21 additions & 10 deletions c/tools/build/Makefile.in
Expand Up @@ -8,10 +8,10 @@
# looking for such non existent files).
.PHONY: all clean realclean test test01 test02

# The compiled Configure.c will replace specific words between @ signs.
# Configure.c replaces the words between @ signs.
CC = @cc@
EXE = @exe@
O = @out@
O = @outfile@
RM_RF = @rm_rf@
THREADS = @threads@

Expand All @@ -20,17 +20,22 @@ all: test

# Recipes to build executables
t/01-toolchain/01a-cc.exe: t/01-toolchain/01a-cc.c t/Test.h
$(CC) $(O)t/01-toolchain/01a-cc.exe t/01-toolchain/01a-cc.c
$(CC) $(O) t/01-toolchain/01a-cc.exe t/01-toolchain/01a-cc.c
-$(RM_RF) 01a-cc.obj

t/01-toolchain/01b-timing.exe: t/01-toolchain/01b-timing.c t/Test.h
$(CC) $(O)t/01-toolchain/01b-timing.exe t/01-toolchain/01b-timing.c
$(CC) $(O) t/01-toolchain/01b-timing.exe t/01-toolchain/01b-timing.c
-$(RM_RF) 01b-timing.obj

t/01-toolchain/01c-threads.exe: t/01-toolchain/01c-threads.c t/Test.h
$(CC) -pthread $(O)t/01-toolchain/01c-threads.exe t/01-toolchain/01c-threads.c
$(CC) -pthread $(O) t/01-toolchain/01c-threads.exe t/01-toolchain/01c-threads.c
-$(RM_RF) 01c-threads.obj

t/02-components/02a-threads.exe: t/02-components/02a-threads.c \
src/threads.h src/threads.c t/Test.h
$(CC) $(O)t/02-components/02a-threads.exe src/threads.c t/02-components/02a-threads.c
-$(RM_RF) threads.obj 02a-threads.obj

t/02-components/02a-hashtable.exe: t/02-components/02a-hashtable.c \
src/hashtable.h src/hashtable.c t/Test.h
$(CC) $(O)t/02-components/02a-hashtable.exe src/hashtable.c t/02-components/02a-hashtable.c
Expand All @@ -45,21 +50,27 @@ tools/build/prove$(EXE): tools/build/prove.c
$(CC) $(O)tools/build/prove$(EXE) tools/build/prove.c
-$(RM_RF) prove.obj

# Test executables are named *.exe even on Unix so that prove can find them
# Note: test executables are named *.exe on all operating systems so
# that prove can find them
test: test01 test02

# The test01 target checks that the C compiler and so on perform the
# functions needed by the rest of the code. It is generally unnecessary
# to run this test, but is useful when troubleshooting.
test01: t/01-toolchain/01a-cc.exe t/01-toolchain/01b-timing.exe \
t/01-toolchain/01c-threads.exe tools/build/prove$(EXE)
tools/build/prove -e "" --ext ".exe" t/01-toolchain

# The test02 target validates the internal libraries of 6model/c
test02: t/02-components/02a-hashtable.exe tools/build/prove$(EXE)
tools/build/prove -e "" --ext ".exe" t/02-components

test: test01 test02

# Miscellaneous targets
clean:
$(RM_RF) *.exe *.obj *.dll *.lib *.exp \
$(RM_RF) *.exe *.obj *.dll *.lib *.exp src/*~ \
tools/build/Configure$(EXE) tools/build/prove$(EXE) \
t/01-toolchain/*.exe t/01-components/*.exe
tools/build/*~ t/01-toolchain/*.exe t/01-toolchain/*~ \
t/02-components/*.exe t/02-components/*~

realclean: clean
perl -MExtUtils::Command -e rm_f Makefile
Expand Down

0 comments on commit 2bed502

Please sign in to comment.