Skip to content

Commit

Permalink
Merge pull request #6230 from tkelman/msvc
Browse files Browse the repository at this point in the history
Steps towards MSVC compatibility
  • Loading branch information
vtjnash committed Apr 1, 2014
2 parents 066e1fd + 431a189 commit 1a37f0a
Show file tree
Hide file tree
Showing 69 changed files with 670 additions and 73 deletions.
25 changes: 23 additions & 2 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,18 @@ USE_COPY_STACKS = 1

# Compiler specific stuff

ifeq ($(USEMSVC), 1)
USEGCC = 0
USECLANG = 0
else
ifeq ($(CC), clang)
USEGCC ?= 0
USECLANG ?= 1
else
USEGCC ?= 1
USECLANG ?= 0
endif
endif

STDLIBCPP_FLAG =

Expand Down Expand Up @@ -239,10 +244,21 @@ endif

JFFLAGS = -O2 $(fPIC)
JF2CFLAGS = -ff2c -fno-second-underscore
ifneq ($(USEMSVC),1)
CPP = $(CC) -E
AR := $(CROSS_COMPILE)ar
AS := $(CROSS_COMPILE)as
LD := $(CROSS_COMPILE)ld
else
CPP = $(CC) -EP
AR := lib
ifeq ($(ARCH),x86_64)
AS := ml64
else
AS := ml
endif
LD := link
endif
RANLIB := $(CROSS_COMPILE)ranlib

ifeq ($(JULIA_CPU_TARGET),native)
Expand Down Expand Up @@ -453,7 +469,7 @@ endif
ifeq ($(OS), Darwin)
WHOLE_ARCHIVE = -Xlinker -all_load
NO_WHOLE_ARCHIVE =
else
else ifneq ($(USEMSVC), 1)
WHOLE_ARCHIVE = -Wl,--whole-archive
NO_WHOLE_ARCHIVE = -Wl,--no-whole-archive
endif
Expand All @@ -479,13 +495,18 @@ JLDFLAGS =
endif

ifeq ($(OS), WINNT)
ifneq ($(USEMSVC), 1)
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -lssp
JCPPFLAGS += -D_WIN32_WINNT=0x0600
JLDFLAGS = -Wl,--stack,8388608
ifeq ($(ARCH),i686)
JLDFLAGS += -Wl,--large-address-aware
endif
else
OSLIBS += kernel32.lib ws2_32.lib psapi.lib advapi32.lib iphlpapi.lib shell32.lib winmm.lib
JLDFLAGS =
endif
JCPPFLAGS += -D_WIN32_WINNT=0x0600
UNTRUSTED_SYSTEM_LIBM = 1
endif

Expand Down
14 changes: 10 additions & 4 deletions base/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ PCRE_CONST = 0x[0-9a-fA-F]+|[-+]?\s*[0-9]+

TAGGED_RELEASE_BANNER = ""

ifneq ($(USEMSVC), 1)
CPP_STDOUT = $(CPP) -P
else
CPP_STDOUT = $(CPP) -E
endif

all: pcre_h.jl errno_h.jl build_h.jl.phony fenv_constants.jl file_constants.jl uv_constants.jl version_git.jl.phony

pcre_h.jl:
Expand All @@ -14,17 +20,17 @@ errno_h.jl:
@$(call PRINT_PERL, echo '#include "errno.h"' | $(CPP) -dM - | perl -nle 'print "const $$1 = int32($$2)" if /^#define\s+(E\w+)\s+(\d+)\s*$$/' | sort > $@)

fenv_constants.jl: ../src/fenv_constants.h
@$(PRINT_PERL) $(CPP) -P -DJULIA ../src/fenv_constants.h | tail -n 8 | perl -ple 's/\sFE_UN\w+/ 0x10/g; s/\sFE_O\w+/ 0x08/g; s/\sFE_DI\w+/ 0x04/g; s/\sFE_INV\w+/ 0x01/g; s/\sFE_TON\w+/ 0x00/g; s/\sFE_UP\w+/ 0x800/g; s/\sFE_DO\w+/ 0x400/g; s/\sFE_TOW\w+/ 0xc00/g' > $@
@$(PRINT_PERL) $(CPP_STDOUT) -DJULIA -I../deps/openlibm/include ../src/fenv_constants.h | tail -n 8 | perl -ple 's/\sFE_UN\w+/ 0x10/g; s/\sFE_O\w+/ 0x08/g; s/\sFE_DI\w+/ 0x04/g; s/\sFE_INV\w+/ 0x01/g; s/\sFE_TON\w+/ 0x00/g; s/\sFE_UP\w+/ 0x800/g; s/\sFE_DO\w+/ 0x400/g; s/\sFE_TOW\w+/ 0xc00/g' > $@

file_constants.jl: ../src/file_constants.h
@$(call PRINT_PERL, $(CPP) -P -DJULIA ../src/file_constants.h | perl -nle 'print "$$1 0o$$2" if /^(\s*const\s+[A-z_]+\s+=)\s+(0[0-9]*)\s*$$/; print "$$1" if /^\s*(const\s+[A-z_]+\s+=\s+([1-9]|0x)[0-9A-z]*)\s*$$/' > $@)
@$(call PRINT_PERL, $(CPP_STDOUT) -DJULIA ../src/file_constants.h | perl -nle 'print "$$1 0o$$2" if /^(\s*const\s+[A-z_]+\s+=)\s+(0[0-9]*)\s*$$/; print "$$1" if /^\s*(const\s+[A-z_]+\s+=\s+([1-9]|0x)[0-9A-z]*)\s*$$/' > $@)

uv_constants.jl: ../src/uv_constants.h $(build_includedir)/uv-errno.h
@$(call PRINT_PERL, $(CPP) -P "-I$(LIBUV_INC)" -DJULIA ../src/uv_constants.h | tail -n 16 > $@)
@$(call PRINT_PERL, $(CPP_STDOUT) "-I$(LIBUV_INC)" -DJULIA ../src/uv_constants.h | tail -n 16 > $@)

build_h.jl.phony:
@echo "# This file is automatically generated in base/Makefile" > $@
@$(CC) -E -P build.h -I../src/support | grep . >> $@
@$(CPP_STDOUT) build.h -I../src/support | grep . >> $@
@echo "const ARCH = :$(ARCH)" >> $@
ifeq ($(OS),$(BUILD_OS))
@echo "const MACHINE = \"$(BUILD_MACHINE)\"" >> $@
Expand Down
20 changes: 16 additions & 4 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ ifneq ($(XC_HOST),)
CONFIGURE_COMMON += --host=$(XC_HOST)
endif
ifeq ($(OS),WINNT)
ifneq ($(USEMSVC), 1)
CONFIGURE_COMMON += LDFLAGS=-Wl,--stack,8388608
endif
endif
CONFIGURE_COMMON += F77="$(FC)" CC="$(CC)" CXX="$(CXX)"

# If the top-level Makefile is called with environment variables,
Expand Down Expand Up @@ -403,6 +405,11 @@ install-llvm: $(LLVM_OBJ_TARGET)

UV_SRC_TARGET = libuv/.libs/libuv.a
UV_OBJ_TARGET = $(build_libdir)/libuv.a
ifneq ($(USEMSVC), 1)
UV_OPTS =
else
UV_OPTS = CFLAGS=-DBUILDING_UV_SHARED
endif

libuv/configure:
(cd .. && git submodule init && git submodule update)
Expand All @@ -414,7 +421,7 @@ libuv/config.status: $(JULIAHOME)/.git/modules/deps/libuv/HEAD
endif
libuv/config.status: libuv/configure
cd libuv && \
./configure --with-pic $(CONFIGURE_COMMON)
./configure --with-pic $(CONFIGURE_COMMON) $(UV_OPTS)
touch -c $@
$(UV_SRC_TARGET): libuv/config.status
touch -c libuv/aclocal.m4
Expand Down Expand Up @@ -613,9 +620,13 @@ install-openspecfun: $(OPENSPECFUN_OBJ_TARGET)
LIBRANDOM_OBJ_TARGET = $(build_shlibdir)/librandom.$(SHLIB_EXT)
LIBRANDOM_OBJ_SOURCE = random/librandom.$(SHLIB_EXT)

LIBRANDOM_CFLAGS = $(CFLAGS) -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing \
--param max-inline-insns-single=1800 -Wmissing-prototypes -Wall -std=c99 \
-DDSFMT_MEXP=19937 $(fPIC) -shared -DDSFMT_DO_NOT_USE_OLD_NAMES
LIBRANDOM_CFLAGS = $(CFLAGS) -DNDEBUG -DDSFMT_MEXP=19937 $(fPIC) -DDSFMT_DO_NOT_USE_OLD_NAMES
ifneq ($(USEMSVC), 1)
LIBRANDOM_CFLAGS += -O3 -finline-functions -fomit-frame-pointer -fno-strict-aliasing \
--param max-inline-insns-single=1800 -Wmissing-prototypes -Wall -std=c99 -shared
else
LIBRANDOM_CFLAGS += -Wl,-dll

This comment has been minimized.

Copy link
@tkelman

tkelman Apr 1, 2014

Contributor

For future reference: this flag is an odd mix of GCC style (-Wl) and MSVC style. In normal MSVC use, you would get a dll using cl /LD foo.c. Alternately, cl foo.c /o libfoo.dll /link /dll works as well (with a warning). I use - instead of / to specify option flags because MSYS automatically translates any argument beginning with a forward slash into a file path.

I am relying on the compile wrapper script from automake to translate -L and -l library linking flags from GCC style to MSVC style, so unfortunately I can't use -LD or -link here. The compile script translates compile cl -Wl,-dll foo.c -o libfoo.dll into cl foo.c -o libfoo.dll -link -dll and things work.

endif
ifeq ($(ARCH), x86_64)
LIBRANDOM_CFLAGS += -msse2 -DHAVE_SSE2
endif
Expand Down Expand Up @@ -1150,6 +1161,7 @@ utf8proc-v$(UTF8PROC_VER).tar.gz:
utf8proc-v$(UTF8PROC_VER)/Makefile: utf8proc-v$(UTF8PROC_VER).tar.gz
$(TAR) -xzf $<
patch $@ < utf8proc_Makefile.patch
cd utf8proc-v$(UTF8PROC_VER) && patch < ../utf8proc_msvc.patch
touch -c $@

$(UTF8PROC_OBJ_SOURCE): utf8proc-v$(UTF8PROC_VER)/Makefile
Expand Down
2 changes: 2 additions & 0 deletions deps/random/randmtzig.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
#include <stdio.h>
#include <stddef.h>
#include <time.h>
#ifndef _MSC_VER
#include <sys/time.h>
#endif

#ifdef STANDALONE
#include <stdlib.h>
Expand Down
40 changes: 40 additions & 0 deletions deps/utf8proc_msvc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
diff --git a/utf8proc.c b/utf8proc.c
index ef2d433..e6c51f7 100644
--- a/utf8proc.c
+++ b/utf8proc.c
@@ -536,7 +536,7 @@ ssize_t utf8proc_map(
*dstptr = NULL;
result = utf8proc_decompose(str, strlen, NULL, 0, options);
if (result < 0) return result;
- buffer = malloc(result * sizeof(int32_t) + 1);
+ buffer = (int32_t *) malloc(result * sizeof(int32_t) + 1);
if (!buffer) return UTF8PROC_ERROR_NOMEM;
result = utf8proc_decompose(str, strlen, buffer, result, options);
if (result < 0) {
@@ -550,7 +550,7 @@ ssize_t utf8proc_map(
}
{
int32_t *newptr;
- newptr = realloc(buffer, (size_t)result+1);
+ newptr = (int32_t *) realloc(buffer, (size_t)result+1);
if (newptr) buffer = newptr;
}
*dstptr = (uint8_t *)buffer;
diff --git a/utf8proc.h b/utf8proc.h
index 24a891b..304e227 100644
--- a/utf8proc.h
+++ b/utf8proc.h
@@ -65,8 +65,13 @@ typedef int int32_t;
#else
#define ssize_t int
#endif
+#ifdef __cplusplus
+typedef unsigned char _bool;
+enum {_false, _true};
+#else
typedef unsigned char bool;
enum {false, true};
+#endif
#else
#include <stdbool.h>
#include <inttypes.h>
17 changes: 12 additions & 5 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ SRCS = \
alloc dlload sys init task array dump toplevel jl_uv jlapi profile llvm-simdloop

FLAGS = \
-D_GNU_SOURCE \
-Wall -Wno-strict-aliasing -fno-omit-frame-pointer \
-Iflisp -Isupport -fvisibility=hidden -fno-common \
-D_GNU_SOURCE -Iflisp -Isupport \
-I$(call exec,$(LLVM_CONFIG) --includedir) \
-I$(LIBUV_INC) -I$(build_includedir) -DLIBRARY_EXPORTS
ifneq ($(USEMSVC), 1)
FLAGS += -Wall -Wno-strict-aliasing -fno-omit-frame-pointer -fvisibility=hidden -fno-common
endif

LLVMLINK = $(call exec,$(LLVM_CONFIG) --libs) $(call exec,$(LLVM_CONFIG) --system-libs 2> /dev/null)
ifeq ($(USE_LLVM_SHLIB),1)
Expand Down Expand Up @@ -78,8 +79,14 @@ flisp/libflisp.a: flisp/*.h flisp/*.c support/libsupport.a
flisp/libflisp-debug.a: flisp/*.h flisp/*.c support/libsupport-debug.a
$(MAKE) -C flisp debug

ifneq ($(USEMSVC), 1)
CXXLD = $(CXX) -shared
else
CXXLD = $(LD) -dll -export:jl_setjmp -export:jl_longjmp
endif

$(build_shlibdir)/libjulia-debug.$(SHLIB_EXT): julia.expmap $(DOBJS) flisp/libflisp-debug.a support/libsupport-debug.a $(LIBUV)
@$(call PRINT_LINK, $(CXX) $(DEBUGFLAGS) $(DOBJS) $(RPATH_ORIGIN) -shared -o $@ $(LDFLAGS) $(DEBUG_LIBS))
@$(call PRINT_LINK, $(CXXLD) $(DEBUGFLAGS) $(DOBJS) $(RPATH_ORIGIN) -o $@ $(LDFLAGS) $(DEBUG_LIBS))
$(INSTALL_NAME_CMD)libjulia-debug.$(SHLIB_EXT) $@
libjulia-debug.a: julia.expmap $(DOBJS) flisp/libflisp-debug.a support/libsupport-debug.a
rm -f $@
Expand All @@ -93,7 +100,7 @@ else
endif

$(build_shlibdir)/libjulia.$(SHLIB_EXT): julia.expmap $(OBJS) flisp/libflisp.a support/libsupport.a $(LIBUV)
@$(call PRINT_LINK, $(CXX) $(SHIPFLAGS) $(OBJS) $(RPATH_ORIGIN) -shared -o $@ $(LDFLAGS) $(RELEASE_LIBS) $(SONAME))
@$(call PRINT_LINK, $(CXXLD) $(SHIPFLAGS) $(OBJS) $(RPATH_ORIGIN) -o $@ $(LDFLAGS) $(RELEASE_LIBS) $(SONAME))
$(INSTALL_NAME_CMD)libjulia.$(SHLIB_EXT) $@
libjulia.a: julia.expmap $(OBJS) flisp/libflisp.a support/libsupport.a
rm -f $@
Expand Down
8 changes: 8 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "julia.h"
#include "julia_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

jl_value_t *jl_true;
jl_value_t *jl_false;

Expand Down Expand Up @@ -931,3 +935,7 @@ JL_CALLABLE(jl_f_default_ctor_2)
jl_type_error(((jl_datatype_t*)F)->name->name->name, ft, args[1]);
return jl_new_struct((jl_datatype_t*)F, args[0], args[1]);
}

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "julia.h"
#include "julia_internal.h"

#ifdef __cplusplus
extern "C" {
#endif

// array constructors ---------------------------------------------------------

static inline int store_unboxed(jl_value_t *el_type)
Expand Down Expand Up @@ -711,3 +715,7 @@ DLLEXPORT void jl_cell_1d_push2(jl_array_t *a, jl_value_t *b, jl_value_t *c)
jl_cellset(a, jl_array_dim(a,0)-2, b);
jl_cellset(a, jl_array_dim(a,0)-1, c);
}

#ifdef __cplusplus
}
#endif
14 changes: 14 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
#include "julia_internal.h"
#include "flisp.h"

#ifdef __cplusplus
extern "C" {
#endif

// MSVC complains about "julia_flisp.boot.inc : error C4335: Mac file format
// detected: please convert the source file to either DOS or UNIX format"
#ifdef _MSC_VER
#pragma warning(disable:4335)
#endif

static uint8_t flisp_system_image[] = {
#include "julia_flisp.boot.inc"
};
Expand Down Expand Up @@ -854,3 +864,7 @@ jl_value_t *jl_prepare_ast(jl_lambda_info_t *li, jl_tuple_t *sparams)
JL_GC_POP();
return ast;
}

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions src/builtin_proto.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef BUILTIN_PROTO_H
#define BUILTIN_PROTO_H

#ifdef __cplusplus
extern "C" {
#endif

// declarations for julia-callable builtin functions

JL_CALLABLE(jl_f_new_expr);
Expand Down Expand Up @@ -38,4 +42,8 @@ JL_CALLABLE(jl_f_invoke);
JL_CALLABLE(jl_f_task);
JL_CALLABLE(jl_f_yieldto);

#ifdef __cplusplus
}
#endif

#endif
13 changes: 13 additions & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include "julia_internal.h"
#include "builtin_proto.h"

#ifdef __cplusplus
extern "C" {
#endif

// exceptions -----------------------------------------------------------------

DLLEXPORT void jl_error(const char *str)
Expand Down Expand Up @@ -671,6 +675,11 @@ DLLEXPORT int jl_strtod(char *str, double *out)
return 0;
}

// MSVC pre-2013 did not define HUGE_VALF
#ifndef HUGE_VALF
#define HUGE_VALF (1e25f * 1e25f)
#endif

DLLEXPORT int jl_substrtof(char *str, int offset, int len, float *out)
{
char *p;
Expand Down Expand Up @@ -1349,3 +1358,7 @@ DLLEXPORT void jl_breakpoint(jl_value_t* v)
{
// put a breakpoint in you debugger here
}

#ifdef __cplusplus
}
#endif
6 changes: 4 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "platform.h"
#if defined(_OS_WINDOWS_)
#define NOMINMAX
#endif
#include "julia.h"
#include "julia_internal.h"

Expand All @@ -10,7 +13,6 @@
* including <math.h> (or rather its content).
*/
#if defined(_OS_WINDOWS_)
#define NOMINMAX
#include <malloc.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Expand Down Expand Up @@ -443,6 +445,7 @@ static Type *NoopType;

// --- utilities ---

extern "C" {
#if defined(JULIA_TARGET_CORE2)
const char *jl_cpu_string = "core2";
#elif defined(JULIA_TARGET_NATIVE)
Expand All @@ -451,7 +454,6 @@ const char *jl_cpu_string = "native";
#error "Must select julia cpu target"
#endif

extern "C" {
int globalUnique = 0;
}

Expand Down
Loading

0 comments on commit 1a37f0a

Please sign in to comment.