Skip to content

Commit 1fc072b

Browse files
committed
Merge pull request #459 from bendlas/master
Improve tempdir usage in ffi-infer
2 parents 59b560f + b952476 commit 1fc072b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

pixie/ffi-infer.pxi

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,35 @@ return 0;
178178
`(def ~(symbol name)
179179
~(callback-type of-type false)))
180180

181+
(def mkdtemp (ffi-fn libc "mkdtemp" [CCharP] CCharP))
182+
(def unlink (ffi-fn libc "unlink" [CCharP] CInt))
183+
(def rmdir (ffi-fn libc "rmdir" [CCharP] CInt))
184+
(def tempdir-template (str (or (getenv "TMPDIR") "/tmp")
185+
"/ffiXXXXXX"))
181186

182187
(defn run-infer [config cmds]
183-
(io/spit "/tmp/tmp.cpp" (str (start-string)
184-
(apply str (map emit-infer-code
185-
cmds))
186-
(end-string)))
187-
(println @load-paths)
188-
(let [cmd-str (str "c++ "
189-
(apply str (interpose " " pixie.platform/c-flags))
190-
" /tmp/tmp.cpp "
191-
(apply str (map (fn [x] ( str " -I " x " "))
192-
@load-paths))
193-
(apply str " " (interpose " " (:cxx-flags *config*)))
194-
" -o /tmp/a.out && /tmp/a.out")
195-
_ (println cmd-str)
196-
result (read-string (io/run-command cmd-str))
197-
gen (vec (map generate-code cmds result))]
198-
`(do ~@gen)))
188+
(let [tempdir (mkdtemp tempdir-template)
189+
infile (str tempdir "/ffi.cpp")
190+
outfile (str tempdir "/ffi.out")]
191+
(io/spit infile (str (start-string)
192+
(apply str (map emit-infer-code
193+
cmds))
194+
(end-string)))
195+
(println @load-paths)
196+
(let [cmd-str (str "c++ "
197+
(apply str (interpose " " pixie.platform/c-flags))
198+
" " infile " "
199+
(apply str (map (fn [x] ( str " -I " x " "))
200+
@load-paths))
201+
(apply str " " (interpose " " (:cxx-flags *config*)))
202+
" -o " outfile " && " outfile)
203+
_ (println cmd-str)
204+
result (read-string (io/run-command cmd-str))
205+
gen (vec (map generate-code cmds result))]
206+
(unlink infile)
207+
(unlink outfile)
208+
(rmdir tempdir)
209+
`(do ~@gen))))
199210

200211
(defn full-lib-name [library-name]
201212
(if (= library-name "c")

0 commit comments

Comments
 (0)