- (flat_absC_c, flat_absC_ncg) =
- case (maybeToBool opt_ProduceC || opt_D_dump_realC,
- maybeToBool opt_ProduceS || opt_D_dump_asm) of
- (True, False) -> (flat_abstractC, absCNop)
- (False, True) -> (absCNop, flat_abstractC)
- (False, False) -> (absCNop, absCNop)
- (True, True) -> error "ERROR: Can't do both .hc and .s at the same time"
-
- -- C stubs for "foreign export"ed functions.
- stub_c_output_d = pprCode CStyle c_code
- stub_c_output_w = showSDoc stub_c_output_d
-
- -- Header file protos for "foreign export"ed functions.
- stub_h_output_d = pprCode CStyle h_code
- stub_h_output_w = showSDoc stub_h_output_d
-
- c_output_d = dumpRealC flat_absC_c
- c_output_w = (\ f -> writeRealC f flat_absC_c)
-
-
- -- don't use doOutput for dumping the f. export stubs
- -- since it is more than likely that the stubs file will
- -- turn out to be empty, in which case no file should be created.
-outputForeignStubs is_header switch "" = return ()
-outputForeignStubs is_header switch doc_str =
- case switch of
- Nothing -> return ()
- Just fname -> writeFile fname (include_prefix ++ doc_str)
- where
- include_prefix
- | is_header = "#include \"Rts.h\"\n"
- | otherwise = "#include \"RtsAPI.h\"\n"
-
-doOutput switch io_action
- = case switch of
- Nothing -> return ()
- Just fname ->
- openFile fname WriteMode >>= \ handle ->
- io_action handle >>
- hClose handle
+ pp_ilx = ilxGen mod tycons stg_binds
+#endif
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Foreign import/export}
+%* *
+%************************************************************************
+
+\begin{code}
+outputForeignStubs :: DynFlags -> Module -> ModLocation -> ForeignStubs
+ -> IO (Bool, -- Header file created
+ Bool) -- C file created
+outputForeignStubs dflags mod location stubs
+ | NoStubs <- stubs = do
+ -- When compiling External Core files, may need to use stub
+ -- files from a previous compilation
+ stub_c_exists <- doesFileExist stub_c
+ stub_h_exists <- doesFileExist stub_h
+ return (stub_h_exists, stub_c_exists)
+
+ | ForeignStubs h_code c_code _ _ <- stubs
+ = do
+ let
+ stub_c_output_d = pprCode CStyle c_code
+ stub_c_output_w = showSDoc stub_c_output_d
+
+ -- Header file protos for "foreign export"ed functions.
+ stub_h_output_d = pprCode CStyle h_code
+ stub_h_output_w = showSDoc stub_h_output_d
+ -- in
+
+ createDirectoryHierarchy (directoryOf stub_c)
+
+ dumpIfSet_dyn dflags Opt_D_dump_foreign
+ "Foreign export header file" stub_h_output_d
+
+ -- we need the #includes from the rts package for the stub files
+ let rtsid = rtsPackageId (pkgState dflags)
+ rts_includes
+ | ExtPackage pid <- rtsid =
+ let rts_pkg = getPackageDetails (pkgState dflags) pid in
+ concatMap mk_include (includes rts_pkg)
+ | otherwise = []
+ mk_include i = "#include \"" ++ i ++ "\"\n"
+
+ stub_h_file_exists
+ <- outputForeignStubs_help stub_h stub_h_output_w
+ ("#include \"HsFFI.h\"\n" ++ cplusplus_hdr) cplusplus_ftr
+
+ dumpIfSet_dyn dflags Opt_D_dump_foreign
+ "Foreign export stubs" stub_c_output_d
+
+ stub_c_file_exists
+ <- outputForeignStubs_help stub_c stub_c_output_w
+ ("#define IN_STG_CODE 0\n" ++
+ "#include \"Rts.h\"\n" ++
+ rts_includes ++
+ cplusplus_hdr)
+ cplusplus_ftr
+ -- We're adding the default hc_header to the stub file, but this
+ -- isn't really HC code, so we need to define IN_STG_CODE==0 to
+ -- avoid the register variables etc. being enabled.
+
+ return (stub_h_file_exists, stub_c_file_exists)
+ where
+ (stub_c, stub_h) = mkStubPaths dflags mod location
+
+cplusplus_hdr = "#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
+cplusplus_ftr = "#ifdef __cplusplus\n}\n#endif\n"
+
+-- Don't use doOutput for dumping the f. export stubs
+-- since it is more than likely that the stubs file will
+-- turn out to be empty, in which case no file should be created.
+outputForeignStubs_help fname "" header footer = return False
+outputForeignStubs_help fname doc_str header footer
+ = do writeFile fname (header ++ doc_str ++ '\n':footer ++ "\n")
+ return True