1*	$Id: dynamic.sno,v 1.5 2012-03-16 05:21:42 phil Exp $
2*
3*	dynamic.sno -- create dynamicly loadable modules for LOAD()
4*	Phil Budne, November 2005
5
6-include 'host.sno'
7
8****************
9* compile a C source file in a way suitable for dynamic loading
10
11	DEFINE('COMPILE_C_DYNAMIC(SRC)CMD,CC,DL_CFLAGS,COPT')
12						:(COMPILE_C_DYNAMIC_END)
13COMPILE_C_DYNAMIC
14* fail if we don't know how to invoke the compiler!
15	CC = HOST(HOST_CC)				:F(FRETURN)
16	IDENT(CC)					:S(FRETURN)
17	DL_CFLAGS = HOST(HOST_DL_CFLAGS) ' '
18	COPT = HOST(HOST_COPT) ' '
19	CMD = CC ' ' COPT DL_CFLAGS '-c -I' HOST(HOST_INCLUDE_DIR) ' ' SRC
20	TERMINAL = DIFFER(DYNAMIC.DEBUG) CMD
21	HOST(HOST_SYSCMD, CMD)				:F(FRETURN)S(RETURN)
22COMPILE_C_DYNAMIC_END
23
24****************
25* link a dynamicly loadable module given
26* an output filename, object files, and libraries
27
28	DEFINE('LINK_DYNAMIC(OUT,OBJ,LIBS)CMD,DL_LD,DL_EXT,DL_LDFLAGS,DSEP')
29							:(LINK_DYNAMIC_END)
30LINK_DYNAMIC
31* fail if we don't have a command!
32	DL_LD = HOST(HOST_DL_LD)			:F(FRETURN)
33	IDENT(DL_LD)					:S(FRETURN)
34
35* add system local suffix (not always ".so"!)
36	DL_EXT = HOST(HOST_DL_EXT)
37	OUT = DIFFER(DL_EXT) OUT DL_EXT
38
39	DL_LDFLAGS = HOST(HOST_DL_LDFLAGS) ' '
40	CMD = DL_LD ' ' DL_LDFLAGS '-o ' OUT ' ' OBJ ' ' LIBS
41	TERMINAL = DIFFER(DYNAMIC.DEBUG) CMD
42	HOST(HOST_SYSCMD, CMD)				:F(FRETURN)
43
44* return the name of the output file
45	LINK_DYNAMIC = OUT
46
47* some dynamic loaders want to see a dir seperator, so make sure there is one!
48	DSEP = HOST(HOST_DIR_SEP)
49	LINK_DYNAMIC BREAK(DSEP) DSEP		:S(RETURN)
50* prepend "./"
51	LINK_DYNAMIC = "." DSEP LINK_DYNAMIC	:(RETURN)
52LINK_DYNAMIC_END
53
54****************
55* make a dynamic module, given C sources
56*	LIBS may contain both objects
57
58	DEFINE('MAKE_DYNAMIC_MODULE(OUT,SRC,LIBS)OBJ,DL_LD,CC')
59						:(MAKE_DYNAMIC_MODULE_END)
60MAKE_DYNAMIC_MODULE
61* fail early if we don't know how to do the job!
62	DIFFER(HOST(HOST_CC))				:F(FRETURN)
63	DIFFER(HOST(HOST_DL_LD))			:F(FRETURN)
64
65* no sources?
66	IDENT(SRC)					:S(MDM_LINK)
67
68* compile C file(s) -- someday handle other languages?
69	COMPILE_C_DYNAMIC(SRC)				:F(FRETURN)
70
71* get names of object files to load
72* XXX FIXME: make sure .c followed by whitespace!
73* split up on whitespace boundaries,
74	OBJ = SRC
75MDM_LOOP
76	OBJ BREAKX('.') . TMP '.c' = TMP '.o'		:S(MDM_LOOP)
77
78* create dynamicly loadable library (often a shared object file)
79MDM_LINK
80
81* fail if nothing to work on!
82	IDENT(OBJ LIBS)					:S(FRETURN)
83	MAKE_DYNAMIC_MODULE = LINK_DYNAMIC(OUT, OBJ, LIBS) :F(FRETURN)
84							:(RETURN)
85MAKE_DYNAMIC_MODULE_END
86