Skip to content

Commit

Permalink
Fix windows build.
Browse files Browse the repository at this point in the history
  • Loading branch information
Febbe committed Oct 4, 2024
1 parent 6f29814 commit b5a6607
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 135 deletions.
2 changes: 1 addition & 1 deletion platform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ endif
OSTYPE = $(shell $(TOP)/platform.sh ostype)
export OSTYPE

ifneq ($(OSTYPE), $(findstring $(OSTYPE), Linux Darwin Freebsd))
ifneq ($(OSTYPE), $(findstring $(OSTYPE), Linux Darwin Freebsd MSYS Mingw))
$(error OSTYPE environment not recognized: $(OSTYPE))
endif

Expand Down
1 change: 0 additions & 1 deletion src/comp/.ghci
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
:set -package containers
:set -package array
:set -package mtl
:set -package unix
:set -package regex-compat
:set -package bytestring
:set -package directory
Expand Down
143 changes: 92 additions & 51 deletions src/comp/BluesimLoader.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE ForeignFunctionInterface, ScopedTypeVariables, MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface, ScopedTypeVariables, MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances, CPP #-}
module BluesimLoader ( BluesimModel(..)
, loadBluesimModel
, unloadBluesimModel
Expand All @@ -23,8 +23,12 @@ import FileNameUtil(dirName,baseName)
import ErrorUtil(internalError)
import HTcl
import SimCCBlock(pfxModel)

#if !defined(windows_HOST_OS) && !defined(mingw32_HOST_OS)
import System.Posix.DynamicLinker
#else
import System.Win32.DLL
import System.Win32.Types
#endif
import Data.Bits
import Data.List(intercalate, isPrefixOf)
import Data.Int
Expand Down Expand Up @@ -379,8 +383,14 @@ foreign import ccall "dynamic"
-- normal Haskell types, so that it presents an abstract Haskell view
-- of the Bluesim model.

#ifdef mingw32_HOST_OS
type DLType = HINSTANCE
#else
type DLType = DL
#endif

data BluesimModel =
BS { model_so :: DL
BS { model_so :: DLType
, model_hdl :: WordPtr
, sim_hdl :: WordPtr
, current_clock :: BSClock
Expand Down Expand Up @@ -435,6 +445,37 @@ data BluesimModel =
, bk_shutdown :: IO ()
}

#if defined(windows_HOST_OS) || defined(mingw32_HOST_OS)
openSharedObject :: String -> IO DLType
openSharedObject fname = do
loadLibrary fname
#else
openSharedObject :: String -> IO DLType
openSharedObject fname = do
dlopen fname [RTLD_NOW]
#endif

#if defined(windows_HOST_OS) || defined(mingw32_HOST_OS)
loadSymbol :: DLType -> String -> IO (FunPtr a)
loadSymbol dl sym = do
ptr <- getProcAddress dl sym
return (castPtrToFunPtr ptr)
#else
loadSymbol :: DLType -> String -> IO (FunPtr a)
loadSymbol dl sym = do
dlsym dl sym
#endif

#if defined(windows_HOST_OS) || defined(mingw32_HOST_OS)
closeShared :: DLType -> IO ()
closeShared dl = do
freeLibrary dl
#else
closeShared :: DLType -> IO ()
closeShared dl = do
dlclose dl
#endif

-- Routine which dynamically loads a Bluesim .so file and creates
-- a BluesimModel value which allows access to the Bluesim object.
-- Returns Nothing if an error occurs during loading
Expand All @@ -443,54 +484,54 @@ loadBluesimModel :: String -> String -> IO (Maybe BluesimModel)
loadBluesimModel fname top_name = do
-- load the shared object
let fname' = (dirName fname) ++ "/" ++ (baseName fname)
dl <- dlopen fname' [RTLD_NOW]
dl <- openSharedObject fname'
-- lookup symbols in the shared object
c_new_model <- dlsym dl ("new_" ++ pfxModel ++ top_name)
c_bk_init <- dlsym dl "bk_init"
c_bk_now <- dlsym dl "bk_now"
c_bk_set_timescale <- dlsym dl "bk_set_timescale"
c_bk_version <- dlsym dl "bk_version"
c_bk_append_argument <- dlsym dl "bk_append_argument"
c_bk_define_clock <- dlsym dl "bk_define_clock"
c_bk_num_clocks <- dlsym dl "bk_num_clocks"
c_bk_get_nth_clock <- dlsym dl "bk_get_nth_clock"
c_bk_clock_name <- dlsym dl "bk_clock_name"
c_bk_get_clock_by_name <- dlsym dl "bk_get_clock_by_name"
c_bk_clock_initial_value <- dlsym dl "bk_clock_initial_value"
c_bk_clock_first_edge <- dlsym dl "bk_clock_first_edge"
c_bk_clock_duration <- dlsym dl "bk_clock_duration"
c_bk_clock_val <- dlsym dl "bk_clock_val"
c_bk_clock_cycle_count <- dlsym dl "bk_clock_cycle_count"
c_bk_clock_edge_count <- dlsym dl "bk_clock_edge_count"
c_bk_clock_last_edge <- dlsym dl "bk_clock_last_edge"
c_bk_quit_after_edge <- dlsym dl "bk_quit_after_edge"
c_bk_schedule_ui_event <- dlsym dl "bk_schedule_ui_event"
c_bk_remove_ui_event <- dlsym dl "bk_remove_ui_event"
c_bk_set_interactive <- dlsym dl "bk_set_interactive"
c_bk_advance <- dlsym dl "bk_advance"
c_bk_is_running <- dlsym dl "bk_is_running"
c_bk_sync <- dlsym dl "bk_sync"
c_bk_abort_now <- dlsym dl "bk_abort_now"
c_bk_finished <- dlsym dl "bk_finished"
c_bk_exit_status <- dlsym dl "bk_exit_status"
c_bk_top_symbol <- dlsym dl "bk_top_symbol"
c_bk_lookup_symbol <- dlsym dl "bk_lookup_symbol"
c_bk_get_size <- dlsym dl "bk_get_size"
c_bk_get_key <- dlsym dl "bk_get_key"
c_bk_is_module <- dlsym dl "bk_is_module"
c_bk_is_rule <- dlsym dl "bk_is_rule"
c_bk_is_single_value <- dlsym dl "bk_is_single_value"
c_bk_is_value_range <- dlsym dl "bk_is_value_range"
c_bk_peek_symbol_value <- dlsym dl "bk_peek_symbol_value"
c_bk_get_range_min_addr <- dlsym dl "bk_get_range_min_addr"
c_bk_get_range_max_addr <- dlsym dl "bk_get_range_max_addr"
c_bk_peek_range_value <- dlsym dl "bk_peek_range_value"
c_bk_num_symbols <- dlsym dl "bk_num_symbols"
c_bk_get_nth_symbol <- dlsym dl "bk_get_nth_symbol"
c_bk_set_VCD_file <- dlsym dl "bk_set_VCD_file"
c_bk_enable_VCD_dumping <- dlsym dl "bk_enable_VCD_dumping"
c_bk_disable_VCD_dumping <- dlsym dl "bk_disable_VCD_dumping"
c_bk_shutdown <- dlsym dl "bk_shutdown"
c_new_model <- loadSymbol dl ("new_" ++ pfxModel ++ top_name)
c_bk_init <- loadSymbol dl "bk_init"
c_bk_now <- loadSymbol dl "bk_now"
c_bk_set_timescale <- loadSymbol dl "bk_set_timescale"
c_bk_version <- loadSymbol dl "bk_version"
c_bk_append_argument <- loadSymbol dl "bk_append_argument"
c_bk_define_clock <- loadSymbol dl "bk_define_clock"
c_bk_num_clocks <- loadSymbol dl "bk_num_clocks"
c_bk_get_nth_clock <- loadSymbol dl "bk_get_nth_clock"
c_bk_clock_name <- loadSymbol dl "bk_clock_name"
c_bk_get_clock_by_name <- loadSymbol dl "bk_get_clock_by_name"
c_bk_clock_initial_value <- loadSymbol dl "bk_clock_initial_value"
c_bk_clock_first_edge <- loadSymbol dl "bk_clock_first_edge"
c_bk_clock_duration <- loadSymbol dl "bk_clock_duration"
c_bk_clock_val <- loadSymbol dl "bk_clock_val"
c_bk_clock_cycle_count <- loadSymbol dl "bk_clock_cycle_count"
c_bk_clock_edge_count <- loadSymbol dl "bk_clock_edge_count"
c_bk_clock_last_edge <- loadSymbol dl "bk_clock_last_edge"
c_bk_quit_after_edge <- loadSymbol dl "bk_quit_after_edge"
c_bk_schedule_ui_event <- loadSymbol dl "bk_schedule_ui_event"
c_bk_remove_ui_event <- loadSymbol dl "bk_remove_ui_event"
c_bk_set_interactive <- loadSymbol dl "bk_set_interactive"
c_bk_advance <- loadSymbol dl "bk_advance"
c_bk_is_running <- loadSymbol dl "bk_is_running"
c_bk_sync <- loadSymbol dl "bk_sync"
c_bk_abort_now <- loadSymbol dl "bk_abort_now"
c_bk_finished <- loadSymbol dl "bk_finished"
c_bk_exit_status <- loadSymbol dl "bk_exit_status"
c_bk_top_symbol <- loadSymbol dl "bk_top_symbol"
c_bk_lookup_symbol <- loadSymbol dl "bk_lookup_symbol"
c_bk_get_size <- loadSymbol dl "bk_get_size"
c_bk_get_key <- loadSymbol dl "bk_get_key"
c_bk_is_module <- loadSymbol dl "bk_is_module"
c_bk_is_rule <- loadSymbol dl "bk_is_rule"
c_bk_is_single_value <- loadSymbol dl "bk_is_single_value"
c_bk_is_value_range <- loadSymbol dl "bk_is_value_range"
c_bk_peek_symbol_value <- loadSymbol dl "bk_peek_symbol_value"
c_bk_get_range_min_addr <- loadSymbol dl "bk_get_range_min_addr"
c_bk_get_range_max_addr <- loadSymbol dl "bk_get_range_max_addr"
c_bk_peek_range_value <- loadSymbol dl "bk_peek_range_value"
c_bk_num_symbols <- loadSymbol dl "bk_num_symbols"
c_bk_get_nth_symbol <- loadSymbol dl "bk_get_nth_symbol"
c_bk_set_VCD_file <- loadSymbol dl "bk_set_VCD_file"
c_bk_enable_VCD_dumping <- loadSymbol dl "bk_enable_VCD_dumping"
c_bk_disable_VCD_dumping <- loadSymbol dl "bk_disable_VCD_dumping"
c_bk_shutdown <- loadSymbol dl "bk_shutdown"
-- convert functions to Haskell types and build BluesimModel
let new_model :: IO WordPtr
new_model = fromC $ dl_ret_ptr c_new_model
Expand Down Expand Up @@ -624,7 +665,7 @@ loadBluesimModel fname top_name = do

unloadBluesimModel :: BluesimModel -> IO ()
unloadBluesimModel bs = do bk_shutdown bs
dlclose (model_so bs)
closeShared (model_so bs)

-- fields are: clock handle, currently active, name, initial value,
-- first edge, low duration, high_duration, cycles elapsed,
Expand Down
17 changes: 11 additions & 6 deletions src/comp/GHC/posix/TmpNam.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module TmpNam(tmpNam, localTmpNam) where
import System.Posix
{-# LANGUAGE CPP #-}

module TmpNam (tmpNam, localTmpNam) where

import System.Directory (getTemporaryDirectory)
import System.Process (getCurrentPid)

tmpNam :: IO String
tmpNam = do
x <- localTmpNam
return $ "/tmp/bsc" ++ x
tmpDir <- getTemporaryDirectory -- Cross-platform temporary directory
x <- localTmpNam
return $ tmpDir ++ "/bsc" ++ x

localTmpNam :: IO String
localTmpNam = do
x <- getProcessID
return $ show x
x <- getCurrentPid
return $ show x
10 changes: 8 additions & 2 deletions src/comp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ WANT_TCL=yes

include $(TOP)/platform.mk

TMPDIR=/tmp
TMPDIR=./tmp

# -----
# System tools
Expand Down Expand Up @@ -127,7 +127,6 @@ PACKAGES = \
-package containers \
-package array \
-package mtl \
-package unix \
-package regex-compat \
-package bytestring \
-package directory \
Expand All @@ -140,8 +139,13 @@ PACKAGES = \
-package syb \
-package integer-gmp \
-package text \
-package signal \
$(GHCEXTRAPKGS)

ifeq ($(OSTYPE), Mingw)
PACKAGES += -package Win32 \
-package unix-compat
endif
# GHC can compile either a single file (use GHCCOMPILEFLAGS) or
# in make mode where it follows dependencies (use GHCMAKEFLAGS).
#
Expand Down Expand Up @@ -291,6 +295,7 @@ endif

.PHONY: bsc
bsc: $(SOURCES)
mkdir -p $(TMPDIR)
$(PREBUILDCOMMAND)
# to force updating of BuildVersion/BuildSystem when necessary
./update-build-version.sh
Expand Down Expand Up @@ -405,5 +410,6 @@ clean:
full_clean: clean
$(RM) -f BuildSystem.hs BuildVersion.hs
$(RM) tags TAGS
$(RM) -rf $(TMPDIR)

# -----
24 changes: 14 additions & 10 deletions src/comp/SimFileUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import Version(bscVersionStr)
import FileNameUtil
import ErrorUtil(internalError)

import System.Posix.Files
import System.Posix.Types(EpochTime)
import System.Directory (doesFileExist, getModificationTime)
import System.FilePath (FilePath)
import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds)
import Data.Time.Clock (UTCTime)

import System.IO(openFile, hGetContents, hClose, IOMode(..))
import Control.Monad(filterM)
import Control.Exception(bracketOnError)
Expand All @@ -23,13 +26,14 @@ import qualified Data.Map as M

-- import Debug.Trace(traceM)

getModTime :: FilePath -> IO (Maybe EpochTime)
getModTime f =
do ok <- fileExist f
if ok
then do s <- getFileStatus f
return $ Just (modificationTime s)
else return Nothing
getModTime :: FilePath -> IO (Maybe POSIXTime)
getModTime f = do
ok <- doesFileExist f
if ok
then do
modTime <- getModificationTime f
return $ Just (utcTimeToPOSIXSeconds modTime)
else return Nothing

codeGenOptionDescr :: Flags -> Bool -> String
codeGenOptionDescr flags is_top =
Expand All @@ -39,7 +43,7 @@ codeGenOptionDescr flags is_top =

readCodeGenOptionDescr :: FilePath -> IO (Maybe String)
readCodeGenOptionDescr f =
do ok <- fileExist f
do ok <- doesFileExist f
if ok
then do bracketOnError (openFile f ReadMode)
(\hdl -> do hClose hdl
Expand Down
4 changes: 2 additions & 2 deletions src/comp/bluetcl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Data.Ord(comparing)
import System.IO.Unsafe(unsafePerformIO)
import System.Environment(getEnv)
import System.Mem(performGC)
import System.Posix.Signals
import System.Signal
import Text.Regex
import Data.Generics (listify)
import qualified Data.Map as M
Expand Down Expand Up @@ -113,7 +113,7 @@ blueshell_Init interp =
-- setup a Ctrl-C handler
mv <- newEmptyMVar
_ <- forkIO (handleCtrlC mv)
_ <- installHandler sigINT (Catch $ recordCtrlC mv) Nothing
_ <- installHandler sigINT (\_ -> recordCtrlC mv)
--
return 0) -- TCL_OK
handler
Expand Down
2 changes: 1 addition & 1 deletion src/comp/bsc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import System.Exit(ExitCode(ExitFailure, ExitSuccess))
import System.FilePath(takeDirectory)
import System.IO(hFlush, stdout, hPutStr, stderr, hGetContents, hClose, hSetBuffering, BufferMode(LineBuffering))
import System.IO(hSetEncoding, utf8)
import System.Posix.Files(fileMode, unionFileModes, ownerExecuteMode, groupExecuteMode, setFileMode, getFileStatus, fileAccess)
import System.PosixCompat.Files(fileMode, unionFileModes, ownerExecuteMode, groupExecuteMode, setFileMode, getFileStatus, fileAccess)
import System.Directory(getDirectoryContents, doesFileExist, getCurrentDirectory)
import System.Time(getClockTime, ClockTime(TOD)) -- XXX: from old-time package
import Data.Char(isSpace, toLower, ord)
Expand Down
8 changes: 7 additions & 1 deletion src/vendor/stp/HaskellIfc/STP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ import qualified Foreign.Concurrent as F
import MVarStrict

import ErrorUtil(internalError)
import System.Posix.Env(getEnvDefault)
import System.Environment (lookupEnv)
--import Util(traceM)


Expand Down Expand Up @@ -170,6 +170,12 @@ checkVersion = do
ptr <- vc_createValidityChecker
return (ptr /= nullPtr)

-- | Cross-platform replacement for 'getEnvDefault'
getEnvDefault :: String -> String -> IO String
getEnvDefault var def = do
value <- lookupEnv var
return $ maybe def id value

------------------------------------------------------------------------
-- Context manipulation

Expand Down
7 changes: 5 additions & 2 deletions src/vendor/stp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ endif

ifeq ($(OSTYPE), Darwin)
SNAME=libstp.dylib
else ifeq ($(OSTYPE), Mingw)
SNAME=libstp.dll
else
SNAME=libstp.so.1
endif
Expand All @@ -21,7 +23,8 @@ all: install

install:
$(MAKE) -C $(SRC) install
ln -fsn HaskellIfc include_hs
rm -rf include_hs
ln -sfn HaskellIfc include_hs
install -m 755 -d $(PREFIX)/lib/SAT
install -m 644 lib/$(SNAME) $(PREFIX)/lib/SAT

Expand All @@ -30,4 +33,4 @@ clean:

full_clean:
$(MAKE) -C $(SRC) full_clean
rm -f include_hs
rm -rf include_hs
Loading

0 comments on commit b5a6607

Please sign in to comment.