Skip to content

Commit

Permalink
make windows build work again
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach DeVito committed Jul 21, 2015
1 parent 767b39a commit 36c35d9
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 34 deletions.
45 changes: 25 additions & 20 deletions msvc/Makefile
Expand Up @@ -6,7 +6,7 @@
# all paths must be ABSOLUTE

LLVM_DIR=C:\Users\zdevito\LLVM
LUAJIT_DIR=C:\Users\zdevito\LuaJIT-2.0.3
LUAJIT_DIR=C:\Users\zdevito\LuaJIT-2.0.4
TERRA_DIR=C:\Users\zdevito\terra
CUDA_DIR=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5

Expand Down Expand Up @@ -49,27 +49,27 @@ LIBPATHS = /LIBPATH:"$(LLVM_DIR)\lib"

CLANG_RESOURCE_DIRECTORY=$(LLVM_DIR)\lib\clang\3.5.0

TERRALIB_SRC = $(SRC)\lctype.cpp $(SRC)\llex.cpp $(SRC)\lobject.cpp $(SRC)\lparser.cpp $(SRC)\lstring.cpp $(SRC)\lzio.cpp $(SRC)\tcompiler.cpp $(SRC)\tcuda.cpp $(SRC)\tcwrapper.cpp $(SRC)\tdebug.cpp $(SRC)\terra.cpp $(SRC)\tinline.cpp $(SRC)\tkind.cpp $(SRC)\tllvmutil.cpp $(SRC)\treadnumber.c
TERRALIB_SRC = $(SRC)\lctype.cpp $(SRC)\llex.cpp $(SRC)\lobject.cpp $(SRC)\lparser.cpp $(SRC)\lstring.cpp $(SRC)\lzio.cpp $(SRC)\tcompiler.cpp $(SRC)\tcuda.cpp $(SRC)\tcwrapper.cpp $(SRC)\tdebug.cpp $(SRC)\terra.cpp $(SRC)\tinline.cpp $(SRC)\tkind.cpp $(SRC)\tllvmutil.cpp $(SRC)\tinternalizedfiles.cpp $(SRC)\treadnumber.c

CPP=cl
LINK=link

LUAJIT = $(LUAJIT_DIR)\src\luajit.exe

CPPFLAGS = /MP8 /nologo /EHsc /w /MD /Zi /D_CRT_SECURE_NO_DEPRECATE /DNOMINMAX /DTERRA_LLVM_HEADERS_HAVE_NDEBUG \
/I$(LUAJIT_DIR)\src /I$(SRC) /I$(BUILD) /I$(TERRA_DIR)\release\include /I$(LLVM_DIR)\include /I$(TERRA_DIR)\msvc \
/I$(LUAJIT_DIR)\src /I$(SRC) /I$(BUILD) /I$(TERRA_DIR)\release\include\terra /I$(LLVM_DIR)\include /I$(TERRA_DIR)\msvc \
/DLLVM_VERSION=35 \
/DTERRA_ENABLE_CUDA /I"$(CUDA_DIR)\include" /I"$(CUDA_DIR)\nvvm\include"

all: $(TERRA_DIR)\release\terra.exe
all: $(TERRA_DIR)\release\bin\terra.exe

$(LUAJIT) $(TERRA_DIR)\release\include\lua.h: $(LUAJIT_DIR)\src\luajit.c
cd $(LUAJIT_DIR)\src
msvcbuild.bat
copy lua.h $(TERRA_DIR)\release\include
copy lualib.h $(TERRA_DIR)\release\include
copy lauxlib.h $(TERRA_DIR)\release\include
copy luaconf.h $(TERRA_DIR)\release\include
copy lua.h $(TERRA_DIR)\release\include\terra
copy lualib.h $(TERRA_DIR)\release\include\terra
copy lauxlib.h $(TERRA_DIR)\release\include\terra
copy luaconf.h $(TERRA_DIR)\release\include\terra

$(BUILD)\terralib.h $(BUILD)\strict.h $(BUILD)\cudalib.h: $(SRC)\terralib.lua $(SRC)\strict.lua $(SRC)\cudalib.lua $(LUAJIT)
set LUA_PATH=$(LUAJIT_DIR)\src\?.lua
Expand All @@ -81,29 +81,34 @@ $(BUILD)\clangpaths.h: $(LUAJIT) $(SRC)\genclangpaths.lua
cd $(TERRA_DIR)
$(LUAJIT) src\genclangpaths.lua build\clangpaths.h $(LLVM_DIR)\bin\clang -I "$(CUDA_DIR:\=/)/include"

$(BUILD)\internalizedfiles.h: $(LUAJIT) $(SRC)\geninternalizedfiles.lua
cd $(TERRA_DIR)
$(LUAJIT) src\geninternalizedfiles.lua build\internalizedfiles.h $(CLANG_RESOURCE_DIRECTORY) "%.h$$" $(CLANG_RESOURCE_DIRECTORY) "%.modulemap$$" $(TERRA_DIR)\lib "%.t$$"

EXPORTS = /EXPORT:terra_init /EXPORT:terra_initwithoptions /EXPORT:terra_load /EXPORT:terra_loadfile /EXPORT:terra_loadbuffer /EXPORT:terra_loadstring /EXPORT:terra_llvmshutdown

$(BUILD)\terra.obj: $(BUILD)\clangpaths.h $(BUILD)\terralib.h $(BUILD)\strict.h $(BUILD)\cudalib.h $(TERRALIB_SRC)
$(BUILD)\terra.obj: $(BUILD)\clangpaths.h $(BUILD)\internalizedfiles.h $(BUILD)\terralib.h $(BUILD)\strict.h $(BUILD)\cudalib.h $(TERRALIB_SRC)
cd $(BUILD)
$(CPP) $(CPPFLAGS) /c $(TERRALIB_SRC)

$(TERRA_DIR)\release\terra.dll: $(BUILD)\terra.obj
cd $(TERRA_DIR)\release
$(TERRA_DIR)\release\bin\terra.dll: $(BUILD)\terra.obj
if not exist $(TERRA_DIR)\release\bin mkdir $(TERRA_DIR)\release\bin
if not exist $(TERRA_DIR)\release\lib mkdir $(TERRA_DIR)\release\lib
cd $(TERRA_DIR)\release\bin
$(LINK) /nologo /MACHINE:X64 /DLL /DEBUG /PDB:terralib.pdb /out:terra.dll /IMPLIB:terra.lib $(BUILD)/*.obj $(LIBS) $(LIBPATHS) $(EXPORTS)
move terra.lib ..\lib\terra.lib

REDIST=$(VCINSTALLDIR)redist\x64\Microsoft.VC120.CRT

$(TERRA_DIR)\release\terra.exe: $(TERRA_DIR)\release\terra.dll $(SRC)\main.cpp
$(TERRA_DIR)\release\bin\terra.exe: $(TERRA_DIR)\release\bin\terra.dll $(SRC)\main.cpp
cd $(TERRA_DIR)
copy $(LUAJIT_DIR)\src\lua51.dll release
copy $(LUAJIT_DIR)\src\lua51.lib release
copy "$(REDIST)\msvcp120.dll" release
copy "$(REDIST)\msvcr120.dll" release
xcopy /E /I /Q /Y $(CLANG_RESOURCE_DIRECTORY) release\include\clang_resource
$(CPP) $(CPPFLAGS) /Fe:$@ $(SRC)\main.cpp $(TERRA_DIR)\msvc\ext\getopt.c $(TERRA_DIR)\msvc\ext\getopt_long.c $(TERRA_DIR)\release\terra.lib $(LUAJIT_DIR)\src\lua51.lib
copy $(LUAJIT_DIR)\src\lua51.dll release\bin
copy $(LUAJIT_DIR)\src\lua51.lib release\lib
copy "$(REDIST)\msvcp120.dll" release\bin
copy "$(REDIST)\msvcr120.dll" release\bin
$(CPP) $(CPPFLAGS) /Fe:$@ $(SRC)\main.cpp $(TERRA_DIR)\msvc\ext\getopt.c $(TERRA_DIR)\msvc\ext\getopt_long.c $(TERRA_DIR)\release\lib\terra.lib $(LUAJIT_DIR)\src\lua51.lib

clean:
del ..\build\*.obj ..\build\*.h ..\build\terra.lib ..\release\terra.exe ..\release\clang.exe
rmdir /s ..\release\include\clang_resource
del ..\build\*.obj ..\build\*.h ..\build\terra.lib ..\release\bin\terra.dll ..\release\lib\terra.lib ..\release\bin\terra.exe

.SUFFIXES:
6 changes: 5 additions & 1 deletion src/geninternalizedfiles.lua
@@ -1,9 +1,13 @@
local outputfilename = arg[1]

local ffi = require("ffi")
local findcmd = ffi.os == "Windows" and "cmd /c dir /b /s \"%s\"" or "find %q"

local listoffiles = {}
for i = 2,#arg,2 do
local path,pattern = arg[i],arg[i+1]
local p = assert(io.popen(("find %q"):format(path)))
local p = assert(io.popen(findcmd:format(path)))
print(findcmd:format(path))
for l in p:lines() do
if l:match(pattern) then
table.insert(listoffiles,{ name = l:sub(#path+1), path = l })
Expand Down
6 changes: 4 additions & 2 deletions src/tcompiler.cpp
Expand Up @@ -317,13 +317,15 @@ int terra_initcompilationunit(lua_State * L) {

static void InitializeJIT(TerraCompilationUnit * CU) {
if(CU->ee) return; //already initialized
Module * topeemodule = (CU->T->options.usemcjit) ? new Module("terra",*CU->T->C->ctx) : CU->M;
#ifdef _WIN32
std::string MCJITTriple = CU->Triple;
MCJITTriple.append("-elf"); //on windows we need to use an elf container because coff is not supported yet
topeemodule->setTargetTriple(MCJITTriple);
#endif
Module * topeemodule = (CU->T->options.usemcjit) ? new Module("terra",*CU->T->C->ctx) : CU->M;
#else
topeemodule->setTargetTriple(CU->Triple);
#endif

std::string err;
EngineBuilder eb(UNIQUEIFY(Module,topeemodule));
eb.setErrorStr(&err)
Expand Down
1 change: 1 addition & 0 deletions src/terra.cpp
Expand Up @@ -201,6 +201,7 @@ static bool pushterrahome(lua_State * L) {
VirtualQuery((void *)terra_init, &mbi, sizeof(mbi));
GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
PathRemoveFileSpecA(path);
PathRemoveFileSpecA(path);
lua_pushstring(L, path);
return true;
}
Expand Down
13 changes: 9 additions & 4 deletions src/terralib.lua
Expand Up @@ -3587,14 +3587,18 @@ end
terra.includepath = os.getenv("INCLUDE_PATH") or "."

local internalizedfiles = {}
local function fileparts(path)
local fileseparators = ffi.os == "Windows" and "\\/" or "/"
local pattern = "[%s]([^%s]*)"
return path:gmatch(pattern:format(fileseparators,fileseparators))
end
function terra.registerinternalizedfiles(names,contents,sizes)
names,contents,sizes = ffi.cast("const char **",names),ffi.cast("uint8_t **",contents),ffi.cast("int*",sizes)
for i = 0,math.huge do
if names[i] == nil then break end
local name,content,size = ffi.string(names[i]),contents[i],sizes[i]
--print(name,size)
local cur = internalizedfiles
for segment in name:gmatch("/([^/]*)") do
for segment in fileparts(name) do
cur.children = cur.children or {}
cur.kind = "directory"
if not cur.children[segment] then
Expand All @@ -3605,9 +3609,10 @@ function terra.registerinternalizedfiles(names,contents,sizes)
cur.contents,cur.size,cur.kind = terra.pointertolightuserdata(content), size, "file"
end
end

local function getinternalizedfile(path)
local cur = internalizedfiles
for segment in path:gmatch("/([^/]*)") do
for segment in fileparts(path) do
if cur.children and cur.children[segment] then
cur = cur.children[segment]
else return end
Expand All @@ -3626,7 +3631,7 @@ end

function terra.includecstring(code,cargs,targetoptions)
local args = terra.newlist {"-O3","-Wno-deprecated","-resource-dir",clangresourcedirectory}
if ffi.os == "Linux" or (targetoptions and targetoptions.Triple and targetoptions.Triple:match("linux")) then
if ffi.os == "Linux" or ffi.os == "Windows" or (targetoptions and targetoptions.Triple and targetoptions.Triple:match("linux")) then
args:insert("-internal-isystem")
args:insert(clangresourcedirectory.."/include")
end
Expand Down
2 changes: 1 addition & 1 deletion tests/classifyfloatstructs.t
Expand Up @@ -81,7 +81,7 @@ add4_val_val:disas()
-- add4_ref_ref_tempvar:disas()


if terralib.lookupsymbol then
if terralib.lookupsymbol and require("ffi").os ~= "Windows" then
terra sizecheck()
var si : terralib.SymbolInfo
terralib.lookupsymbol(add4_val_val,&si)
Expand Down
6 changes: 3 additions & 3 deletions tests/cudaoffline.t
Expand Up @@ -59,6 +59,6 @@ path = terralib.cudahome..path
local args = ffi.os == "Windows" and {path.."\\cuda.lib", path.."\\cudart.lib"}
or {"-L"..path, "-Wl,-rpath,"..path, "-lcuda", "-lcudart"}

terralib.saveobj("cudaoffline",{ main = main },args)
local exe = ffi.os == "Windows" and ".\\cudaoffline" or "./cudaoffline"
assert(os.execute(exe) == 0)
local name = ffi.os == "Windows" and ".\\cudaoffline.exe" or "./cudaoffline"
terralib.saveobj(name,{ main = main },args)
assert(os.execute(name) == 0)
4 changes: 2 additions & 2 deletions tests/dynlib.t
Expand Up @@ -40,8 +40,8 @@ if ffi.os ~= "Windows" then

else
local putenv = terralib.externfunction("_putenv", rawstring -> int)
local flags = {libpath.."\\terra_dynamic.lib",libpath.."\\lua51.lib"}
local flags = {libpath.."\\terra.lib",libpath.."\\lua51.lib"}
terralib.saveobj("dynlib.exe",{main = main},flags)
putenv("Path="..os.getenv("Path")..";"..terralib.terrahome.."\\lib") --make dll search happy
putenv("Path="..os.getenv("Path")..";"..terralib.terrahome.."\\bin") --make dll search happy
assert(0 == os.execute(".\\dynlib.exe"))
end
2 changes: 1 addition & 1 deletion tests/run
Expand Up @@ -12,7 +12,7 @@ local function getcommand(file)
local prefix = terralib and terralib.terrahome and
terralib.terrahome .."/bin/terra" or "../terra"
if ffi.os == "Windows" then
prefix = "cmd /c " .. prefix:gsub("/","\\\\")
prefix = "cmd /c " .. prefix:gsub("[/\\]","\\\\")
end
return prefix
end
Expand Down

0 comments on commit 36c35d9

Please sign in to comment.