hopefully the last revision of the relative paths handling algorithm

This commit is contained in:
krasimir
2009-10-06 10:27:34 +00:00
parent 332dbf7b9b
commit cbcdae9148
8 changed files with 43 additions and 41 deletions

View File

@@ -26,9 +26,10 @@ main = do
setConsoleOutputCP codepage setConsoleOutputCP codepage
#endif #endif
args <- getArgs args <- getArgs
cdir <- getCurrentDirectory case parseOptions args of
case parseOptions cdir args of Ok (opts,files) -> do curr_dir <- getCurrentDirectory
Ok (opts,files) -> mainOpts opts files lib_dir <- getLibraryDirectory opts
mainOpts (fixRelativeLibPaths curr_dir lib_dir opts) files
Bad err -> do hPutStrLn stderr err Bad err -> do hPutStrLn stderr err
hPutStrLn stderr "You may want to try --help." hPutStrLn stderr "You may want to try --help."
exitFailure exitFailure

View File

@@ -114,9 +114,11 @@ compileModule :: Options -- ^ Options from program command line and shell comman
compileModule opts1 env file = do compileModule opts1 env file = do
file <- getRealFile file file <- getRealFile file
opts0 <- getOptionsFromFile file opts0 <- getOptionsFromFile file
let opts = addOptions opts0 opts1 curr_dir <- return $ dropFileName file
let fdir = dropFileName file lib_dir <- ioeIO $ getLibraryDirectory (addOptions opts0 opts1)
ps <- ioeIO $ extendPathEnv opts fdir let opts = addOptions (fixRelativeLibPaths curr_dir lib_dir opts0) opts1
ps0 <- ioeIO $ extendPathEnv opts
let ps = nub (curr_dir : ps0)
ioeIO $ putIfVerb opts $ "module search path:" +++ show ps ---- ioeIO $ putIfVerb opts $ "module search path:" +++ show ps ----
let (_,sgr,rfs) = env let (_,sgr,rfs) = env
files <- getAllFiles opts ps rfs file files <- getAllFiles opts ps rfs file
@@ -125,13 +127,13 @@ compileModule opts1 env file = do
ioeIO $ putIfVerb opts $ "modules to include:" +++ show names ---- ioeIO $ putIfVerb opts $ "modules to include:" +++ show names ----
foldM (compileOne opts) (0,sgr,rfs) files foldM (compileOne opts) (0,sgr,rfs) files
where where
getRealFile file1 = do getRealFile file = do
exists <- ioeIO $ doesFileExist file1 exists <- ioeIO $ doesFileExist file
if exists if exists
then return file then return file
else if isRelative file else if isRelative file
then do libpath <- ioeIO $ getLibraryPath opts1 then do lib_dir <- ioeIO $ getLibraryDirectory opts1
let file1 = libpath </> file let file1 = lib_dir </> file
exists <- ioeIO $ doesFileExist file1 exists <- ioeIO $ doesFileExist file1
if exists if exists
then return file1 then return file1

View File

@@ -205,7 +205,7 @@ getOptionsFromFile file = do
(\_ -> return (Bad $ "File " ++ file ++ " does not exist")) (\_ -> return (Bad $ "File " ++ file ++ " does not exist"))
let ls = filter (BS.isPrefixOf (BS.pack "--#")) $ BS.lines s let ls = filter (BS.isPrefixOf (BS.pack "--#")) $ BS.lines s
fs = map (BS.unpack . BS.unwords . BS.words . BS.drop 3) ls fs = map (BS.unpack . BS.unwords . BS.words . BS.drop 3) ls
ioeErr $ parseModuleOptions (dropFileName file) fs ioeErr $ parseModuleOptions fs
getFilePath :: [FilePath] -> String -> IO (Maybe FilePath) getFilePath :: [FilePath] -> String -> IO (Maybe FilePath)
getFilePath paths file = get paths getFilePath paths file = get paths

View File

@@ -82,7 +82,7 @@ instance Binary ModuleStatus where
instance Binary Options where instance Binary Options where
put = put . optionsGFO put = put . optionsGFO
get = do opts <- get get = do opts <- get
case parseModuleOptions "." ["--" ++ flag ++ "=" ++ value | (flag,value) <- opts] of case parseModuleOptions ["--" ++ flag ++ "=" ++ value | (flag,value) <- opts] of
Ok x -> return x Ok x -> return x
Bad msg -> fail msg Bad msg -> fail msg

View File

@@ -276,7 +276,7 @@ TermDef
FlagDef :: { Options } FlagDef :: { Options }
FlagDef FlagDef
: Posn Ident '=' Ident Posn {% case parseModuleOptions "." ["--" ++ showIdent $2 ++ "=" ++ showIdent $4] of : Posn Ident '=' Ident Posn {% case parseModuleOptions ["--" ++ showIdent $2 ++ "=" ++ showIdent $4] of
Ok x -> return x Ok x -> return x
Bad msg -> failLoc $1 msg } Bad msg -> failLoc $1 msg }

View File

@@ -7,7 +7,7 @@ module GF.Infra.Option
SISRFormat(..), Optimization(..), CFGTransform(..), HaskellOption(..), SISRFormat(..), Optimization(..), CFGTransform(..), HaskellOption(..),
Dump(..), Printer(..), Recomp(..), BuildParser(..), Dump(..), Printer(..), Recomp(..), BuildParser(..),
-- * Option parsing -- * Option parsing
parseOptions, parseModuleOptions, parseOptions, parseModuleOptions, fixRelativeLibPaths,
-- * Option pretty-printing -- * Option pretty-printing
optionsGFO, optionsGFO,
optionsPGF, optionsPGF,
@@ -186,29 +186,27 @@ instance Show Options where
-- Option parsing -- Option parsing
parseOptions :: FilePath -- ^ if there are relative file paths they will be interpreted as relative to this path parseOptions :: [String] -- ^ list of string arguments
-> [String] -- ^ list of string arguments
-> Err (Options, [FilePath]) -> Err (Options, [FilePath])
parseOptions root args parseOptions args
| not (null errs) = errors errs | not (null errs) = errors errs
| otherwise = do opts <- liftM concatOptions $ sequence optss | otherwise = do opts <- liftM concatOptions $ sequence optss
return (fixRelativeLibPaths opts, files) return (opts, files)
where where
(optss, files, errs) = getOpt RequireOrder optDescr args (optss, files, errs) = getOpt RequireOrder optDescr args
fixRelativeLibPaths (Options o) = Options (fixPathFlags . o)
where
fixPathFlags f@(Flags{optLibraryPath=path}) = f{optLibraryPath=map (root </>) path}
parseModuleOptions :: FilePath -- ^ if there are relative file paths they will be interpreted as relative to this path parseModuleOptions :: [String] -- ^ list of string arguments
-> [String] -- ^ list of string arguments
-> Err Options -> Err Options
parseModuleOptions root args = do parseModuleOptions args = do
(opts,nonopts) <- parseOptions root args (opts,nonopts) <- parseOptions args
if null nonopts if null nonopts
then return opts then return opts
else errors $ map ("Non-option among module options: " ++) nonopts else errors $ map ("Non-option among module options: " ++) nonopts
fixRelativeLibPaths curr_dir lib_dir (Options o) = Options (fixPathFlags . o)
where
fixPathFlags f@(Flags{optLibraryPath=path}) = f{optLibraryPath=concatMap (\dir -> [curr_dir </> dir, lib_dir </> dir]) path}
-- Showing options -- Showing options
-- | Pretty-print the options that are preserved in .gfo files. -- | Pretty-print the options that are preserved in .gfo files.

View File

@@ -57,8 +57,8 @@ type FullPath = String
gfLibraryPath = "GF_LIB_PATH" gfLibraryPath = "GF_LIB_PATH"
gfGrammarPathVar = "GF_GRAMMAR_PATH" gfGrammarPathVar = "GF_GRAMMAR_PATH"
getLibraryPath :: Options -> IO FilePath getLibraryDirectory :: Options -> IO FilePath
getLibraryPath opts = getLibraryDirectory opts =
case flag optGFLibPath opts of case flag optGFLibPath opts of
Just path -> return path Just path -> return path
Nothing -> catch Nothing -> catch
@@ -66,19 +66,19 @@ getLibraryPath opts =
(\ex -> getDataDir >>= \path -> return (path </> "lib")) (\ex -> getDataDir >>= \path -> return (path </> "lib"))
getGrammarPath :: FilePath -> IO [FilePath] getGrammarPath :: FilePath -> IO [FilePath]
getGrammarPath lib_path = do getGrammarPath lib_dir = do
catch (fmap splitSearchPath $ getEnv gfGrammarPathVar) (\_ -> return [lib_path </> "prelude"]) -- e.g. GF_GRAMMAR_PATH catch (fmap splitSearchPath $ getEnv gfGrammarPathVar) (\_ -> return [lib_dir </> "prelude"]) -- e.g. GF_GRAMMAR_PATH
-- | extends the search path with the -- | extends the search path with the
-- 'gfLibraryPath' and 'gfGrammarPathVar' -- 'gfLibraryPath' and 'gfGrammarPathVar'
-- environment variables. Returns only existing paths. -- environment variables. Returns only existing paths.
extendPathEnv :: Options -> FilePath -> IO [FilePath] extendPathEnv :: Options -> IO [FilePath]
extendPathEnv opts fdir = do extendPathEnv opts = do
opt_paths <- return $ flag optLibraryPath opts -- e.g. paths given as options opt_path <- return $ flag optLibraryPath opts -- e.g. paths given as options
lib_path <- getLibraryPath opts -- e.g. GF_LIB_PATH lib_dir <- getLibraryDirectory opts -- e.g. GF_LIB_PATH
grm_paths <- getGrammarPath lib_path -- e.g. GF_GRAMMAR_PATH grm_path <- getGrammarPath lib_dir -- e.g. GF_GRAMMAR_PATH
let paths = opt_paths ++ [lib_path] ++ grm_paths let paths = opt_path ++ [lib_dir] ++ grm_path
ps <- liftM (nub . concat) $ mapM allSubdirs paths ps <- liftM concat $ mapM allSubdirs paths
mapM canonicalizePath ps mapM canonicalizePath ps
where where
allSubdirs :: FilePath -> IO [FilePath] allSubdirs :: FilePath -> IO [FilePath]

View File

@@ -130,10 +130,11 @@ loop opts gfenv0 = do
putStrLn "wrote graph in file _gfdepgraph.dot" putStrLn "wrote graph in file _gfdepgraph.dot"
loopNewCPU gfenv loopNewCPU gfenv
"i":args -> do "i":args -> do
cdir <- getCurrentDirectory gfenv' <- case parseOptions args of
gfenv' <- case parseOptions cdir args of Ok (opts',files) -> do
Ok (opts',files) -> curr_dir <- getCurrentDirectory
importInEnv gfenv (addOptions opts opts') files lib_dir <- getLibraryDirectory (addOptions opts opts')
importInEnv gfenv (addOptions opts (fixRelativeLibPaths curr_dir lib_dir opts')) files
Bad err -> do Bad err -> do
putStrLn $ "Command parse error: " ++ err putStrLn $ "Command parse error: " ++ err
return gfenv return gfenv