Skip to content

Commit

Permalink
Add OS Detection capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed May 13, 2012
1 parent 94a3286 commit fbf1348
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions base/.gitignore
@@ -1,3 +1,4 @@
/pcre_h.jl
/errno_h.jl
os_detect.jl
/build_h.jl
8 changes: 6 additions & 2 deletions base/Makefile
Expand Up @@ -3,15 +3,19 @@ include ../Make.inc

PCRE_CONST = 0x[0-9a-fA-F]+|[-+]?\s*[0-9]+

all: pcre_h.jl errno_h.jl
all: pcre_h.jl errno_h.jl os_detect.jl

pcre_h.jl:
$(QUIET_PERL) cpp -dM $(shell $(PCRE_CONFIG) --prefix)/include/pcre.h | perl -nle '/^\s*#define\s+(PCRE\w*)\s*\(?($(PCRE_CONST))\)?\s*$$/ and print "const $$1 = int32($$2)"' | sort > $@
$(QUIET_PERL) ${CC} -E -dM $(shell $(PCRE_CONFIG) --prefix)/include/pcre.h | perl -nle '/^\s*#define\s+(PCRE\w*)\s*\(?($(PCRE_CONST))\)?\s*$$/ and print "const $$1 = int32($$2)"' | sort > $@

errno_h.jl:
$(QUIET_PERL) echo '#include "errno.h"' | cpp -dM - | perl -nle 'print "const $$1 = int32($$2)" if /^#define\s+(E\w+)\s+(\d+)\s*$$/' | sort > $@

os_detect.jl: ../src/os_detect.h
$(QUIET_PERL) ${CC} -E -P -DJULIA ../src/os_detect.h | perl -p -e 's/\\n/\n/g' > $@

clean:
rm -f *# *~
rm -f pcre_h.jl
rm -f errno_h.jl
rm -f os_detect.jl
17 changes: 17 additions & 0 deletions base/osutils.jl
@@ -0,0 +1,17 @@
include("os_detect.jl")

macro windows_only(ex)
if(CURRENT_OS == JL_OS_Windows)
return ex
else
return :nothing
end
end

macro unix_only(ex)
if(CURRENT_OS != JL_OS_Windows)
return ex
else
return :nothing
end
end
1 change: 1 addition & 0 deletions base/sysimg.jl
Expand Up @@ -47,6 +47,7 @@ include("grisu.jl")
include("printf.jl")

# system & environment
include("osutils.jl")
include("libc.jl")
include("env.jl")
include("errno_h.jl")
Expand Down
102 changes: 102 additions & 0 deletions src/os_detect.h
@@ -0,0 +1,102 @@
#ifndef OS_DETECT_H
#define OS_DETECT_H

/* This file uses is used by both Julia and C */

/* LOGIC PRIMITIVES */
#define JL_BOOL_0 0
#define JL_BOOL_1 1
#define JL_BOOL_2 1
#define JL_BOOL_3 1
#define JL_BOOL_4 1
#define JL_BOOL_5 1
#define JL_BOOL_6 1
#define JL_BOOL_7 1
#define JL_BOOL_8 1
#define JL_BOOL_9 1
#define JL_BOOL(x) JL_BOOL_##x
#define I(x) x

#define JL_IF_0(T,F) F
#define JL_IF_1(T,F) T
#define JL_IF2(C,T,F) JL_IF_##C(T,F)
#define JL_IF(C,T,F) JL_IF2(C,T,F)

/* OS MAP - to add an OS just append entry to map.
All other functions will be updated automagically, but detection by variables must be added to jl_current_os
X(NUM,C-Var,Julia name) - General INFO
XX(ISUNIX) - OS Traits
*/
#define NUM_OS = 4
#define NOP(x)
#define JL_OS_MAP2(X,XX) \
X(0,Windows) XX(0) \
X(1,Linux) XX(1) \
X(2,FreeBSD) XX(1) \
X(3,OSX) XX(1)
#define JL_OS_MAP(X) JL_OS_MAP2(X,NOP)
#define OS_INDEX_MAP(x) x

#if defined(__WIN32__)
#define OS_CURRENT Windows
#elif defined(__linux)
#define OS_CURRENT Linux
#elif defined(__FreeBSD__)
#define OS_CURRENT FreeBSD
#elif defined(__APPLE__)
#define OS_CURRENT OSX
#else
#define OS_CURRENT Unknown
#endif

#ifndef JULIA
#define OS_GEN(NUM,NAME) NAME=NUM,
enum OS {
JL_OS_MAP(OS_GEN)
};

#undef OS_GEN
#define OS_NAME_GEN(NUM,NAME) #NAME ,
const char *OS_NAME[NUM_OS] {
JL_OS_MAP(OS_NAME_GEN)
};
#undef OS_NAME_GEN
#define OS_CUR_GEN(NUM,NAME) JL_IF(VAR,return NUM,)
DLLEXPORT const unsigned char jl_current_os() const
{

}

#define OS_CASE(NUM,NAME) case NUM:
#define SWITCH_BODY(num,CASE,BODY) \
{ \
switch(num) { \
JL_OS_MAP2(OS_CASE,MACRO) \
} \
} \

#define OS_ISUNIX(ISUNIX) return ISUNIX;
DLLEXPORT char jl_is_unix(const unsigned char osnum)
SWITCH_BODY(osnum,OS_CASE,OS_ISUNIX)

const char *jl_os_name(const unsigned char osnum) const
{
return OS_NAME[OS_INDEX_MAP(osnum)]
}
#else
#define PREFIX2(NAME) JL_OS_##NAME
#define PREFIX(NAME) PREFIX2(NAME)
#define CONSTANT_MAP(NUM,NAME) const JL_OS_##NAME = uint8(NUM);\n
JL_OS_MAP(CONSTANT_MAP)
const CURRENT_OS = PREFIX(OS_CURRENT);

#define OS_NAME_IFELSE(NUM,NAME) JL_IF(JL_BOOL(NUM),elseif,if) (osnum==PREFIX(NAME)) return #NAME; \n
function _jl_os_name(osnum::Uint8)

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson May 14, 2012

Member

Could we use symbols for these so we don't need a function to get a human-readable representation?

This comment has been minimized.

Copy link
@Keno

Keno May 14, 2012

Author Member

That should work. I'll try it out.

This comment has been minimized.

Copy link
@Keno

Keno May 15, 2012

Author Member

Would you prefer :JL_OS_Linux or just :Linux (I'm tending towards the latter)

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson May 15, 2012

Member

yes the latter

This comment has been minimized.

Copy link
@Keno

Keno May 15, 2012

Author Member

Done in 418f0ef

JL_OS_MAP(OS_NAME_IFELSE)
else
return "Unknown"
end
end
_jl_os_name(osnum::Integer) = _jl_os_name(uint8(osnum))
#endif
#endif // OS_DETECT_H

0 comments on commit fbf1348

Please sign in to comment.