--- gforth/libcc.fs 2008/07/13 23:53:08 1.46 +++ gforth/libcc.fs 2009/10/17 20:57:33 1.60 @@ -1,6 +1,6 @@ \ libcc.fs foreign function interface implemented using a C compiler -\ Copyright (C) 2006,2007 Free Software Foundation, Inc. +\ Copyright (C) 2006,2007,2008 Free Software Foundation, Inc. \ This file is part of Gforth. @@ -135,9 +135,8 @@ \ : delete-file 2drop 0 ; require struct.fs +require mkdir.fs - \ counted-string - \ c-function-ft word body: struct cell% field cff-cfr \ xt of c-function-rt word @@ -161,7 +160,8 @@ variable lib-handle-addr \ points to the 2variable libcc-named-dir-v \ directory for named libcc wrapper libraries 0 value libcc-path \ pointer to path of library directories -: delete-file 2drop 0 ; +defer replace-rpath ( c-addr1 u1 -- c-addr2 u2 ) +' noop is replace-rpath : .nb ( n -- ) 0 .r ; @@ -219,24 +219,19 @@ end-struct list% node list-next @ repeat ; -\ linked libraries +2variable c-libs \ library names in a string (without "lib") -list% - cell% 2* field c-lib-string -end-struct c-lib% - -variable c-libs \ linked list of library names (without "lib") - -: add-lib ( c-addr u -- ) +: add-lib ( c-addr u -- ) \ gforth \G Add library lib@i{string} to the list of libraries, where -\G @i{string} is represented by @i{c-addr u}. - c-lib% %size allocate throw dup >r - c-lib-string 2! - r> c-libs list-insert ; - -: append-l ( c-addr1 u1 node -- c-addr2 u2 ) - \ append " -l" to string1 - >r s" -l" append r> c-lib-string 2@ append ; + \G @i{string} is represented by @i{c-addr u}. + c-libs 2@ d0= IF 0 allocate throw 0 c-libs 2! THEN + c-libs 2@ s" -l" append 2swap append c-libs 2! ; + +: add-libpath ( c-addr u -- ) \ gforth +\G Add path @i{string} to the list of library search pathes, where + \G @i{string} is represented by @i{c-addr u}. + c-libs 2@ d0= IF 0 allocate throw 0 c-libs 2! THEN + c-libs 2@ s" -L" append 2swap append c-libs 2! ; \ C prefix lines @@ -256,6 +251,9 @@ variable c-prefix-lines-end c-prefix-lin : print-c-prefix-lines ( -- ) c-prefix-lines @ ['] print-c-prefix-line list-map ; +: free-c-prefix-lines ( -- ) + c-prefix-lines @ off c-prefix-lines @ c-prefix-lines-end ! ; + : save-c-prefix-line ( c-addr u -- ) c-source-file-id @ ?dup-if >r 2dup r> write-line throw @@ -432,7 +430,7 @@ create gen-wrapped-types dup { descriptor } count { ret } count 2dup { d: pars } chars + count { d: c-name } ." void " lib-modulename 2@ type ." _LTX_" descriptor wrapper-function-name 2dup type drop free throw - .\" (void)\n" + .\" (GFORTH_ARGS)\n" .\" {\n Cell MAYBE_UNUSED *sp = gforth_SP;\n Float MAYBE_UNUSED *fp = gforth_FP;\n " pars c-name 2over count-stacks ret gen-wrapped-stmt .\" ;\n" ?dup-if @@ -497,7 +495,7 @@ create gen-wrapped-types lib-filename 2@ s" .c" s+ 2dup w/o create-file throw dup c-source-file-id ! ['] print-c-prefix-lines swap outfile-execute - drop free throw ; + drop free-c-prefix-lines free throw ; : c-named-library-name ( c-addr u -- ) \ set up filenames for a (possibly new) library; c-addr u is the @@ -506,13 +504,15 @@ create gen-wrapped-types open-wrappers dup if lib-handle-addr @ ! else + libcc-named-dir $1ff mkdir-parents drop drop c-library-name-create endif ; : c-tmp-library-name ( c-addr u -- ) \ set up filenames for a new library; c-addr u is the basename of \ the library - libcc-tmp-dir prepend-dirname c-library-name-setup c-library-name-create ; + libcc-tmp-dir 2dup $1ff mkdir-parents drop + prepend-dirname c-library-name-setup c-library-name-create ; : lib-handle ( -- addr ) lib-handle-addr @ @ ; @@ -554,18 +554,20 @@ DEFER compile-wrapper-function ( -- ) lib-handle 0= if c-source-file close-file throw 0 c-source-file-id ! - [ libtool-command s" --silent --mode=compile 'gcc-4.2 -arch x86_64' -I " s+ - s" includedir" getenv append ] sliteral + [ libtool-command s" --silent --mode=compile " s+ + libtool-cc append s" -I '" append + s" includedir" getenv append s" '" append ] sliteral s" -O -c " s+ lib-filename 2@ append s" .c -o " append lib-filename 2@ append s" .lo" append ( c-addr u ) - cr 2dup type + \ 2dup type cr 2dup system drop free throw $? abort" libtool compile failed" - [ libtool-command s" --silent --mode=link 'gcc-4.2 -arch x86_64' -module -rpath " s+ ] sliteral - lib-filename 2@ dirname s+ s" " append + [ libtool-command s" --silent --mode=link " s+ + libtool-cc append libtool-flags append s" -module -rpath " s+ ] sliteral + lib-filename 2@ dirname replace-rpath s+ s" " append lib-filename 2@ append s" .lo -o " append lib-filename 2@ append s" .la" append ( c-addr u ) - c-libs @ ['] append-l list-map - 2dup type cr + c-libs 2@ append + \ 2dup type cr 2dup system drop free throw $? abort" libtool link failed" open-wrappers dup 0= if .lib-error true abort" open-lib failed" @@ -613,12 +615,12 @@ DEFER compile-wrapper-function ( -- ) defer lastxt dup c-function-rt lastxt c-function-ft lastxt swap defer! ; -: clear-libs ( -- ) +: clear-libs ( -- ) \ gforth \G Clear the list of libs c-source-file-id @ if compile-wrapper-function endif - 0 c-libs ! ; + 0. c-libs 2! ; clear-libs : c-library-incomplete ( -- )