fsxs.in 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/usr/bin/perl
  2. #
  3. # FreeSWITCH fsxs
  4. #
  5. # (C) 2006-2008 Stefan Knoblich <stkn@netdomination.org>
  6. use strict;
  7. #
  8. # @FOO@ will be replaced by the freeswitch build process
  9. #
  10. my %vars = (
  11. CC => '@CC@',
  12. LD => '@LD@',
  13. MKDIR => '@MKINSTALLDIRS@',
  14. INSTALL => '@INSTALL@',
  15. LIBS => '@LIBS@',
  16. CFLAGS => '@CFLAGS@',
  17. INCLUDES => '@INCLUDES@',
  18. LDFLAGS => '@LDFLAGS@',
  19. SOLINK => '@SOLINK@',
  20. MODULESDIR => '@MODULES_DIR@',
  21. LIBDIR => '@LIB_DIR@',
  22. BINDIR => '@BIN_DIR@',
  23. INCLUDEDIR => '@INC_DIR@',
  24. DBDIR => '@DB_DIR@',
  25. CONFDIR => '@CFG_DIR@',
  26. PREFIX => '@PREFIX@'
  27. );
  28. #
  29. # Misc variables
  30. #
  31. my @files = ();
  32. my $command;
  33. my $target;
  34. my $needs_target = 1;
  35. my $debug = 0;
  36. #
  37. # functions
  38. #
  39. sub do_exec {
  40. my $retval = system( "@_ >/dev/null" );
  41. if( $retval ) {
  42. exit $retval;
  43. }
  44. }
  45. sub fsxs_usage {
  46. print "FreeSWITCH fsxs\n(C) 2006-2008 Stefan Knoblich <stkn\@netdomination.org>\n";
  47. print "\n";
  48. print "Usage:\n";
  49. print "\t$0 compile [options] <file1 ...>\n";
  50. print "\t$0 link [options] <target> <file1 ...>\n";
  51. print "\t$0 build [options] <target> <file1 ...>\n";
  52. print "\t$0 install [options] <file1 ...>\n\n";
  53. print "\t$0 show <var1 ...varN>\n";
  54. print "\t$0 <--var1 ...--varN>\n\n";
  55. print "Command description:\n";
  56. print "\tcompile: Compile source file(s) into object file(s)\n";
  57. print "\tlink: Create module from object file(s)\n";
  58. print "\tbuild: Build module from source file(s) (compile + link)\n";
  59. print "\tinstall: Install module(s) into FreeSWITCH module directory\n";
  60. print "\tshow: Show defined variable(s)\n";
  61. print "\n";
  62. print "Options:\n";
  63. print "\t--add-cflags Append custom cflags [compile, build]\n";
  64. print "\t--set-cflags Override cflags [compile, build]\n";
  65. print "\n";
  66. print "\t--add-ldflags Append custom ldflags [link, build]\n";
  67. print "\t--set-ldflags Override ldflags [link, build]\n";
  68. print "\t--add-libs Append additional libs [link, build]\n";
  69. print "\t--set-libs Override libs [link, build]\n";
  70. print "\n";
  71. print "\t--destdir Installation prefix [install]\n";
  72. print "\n";
  73. print "Variable names for \"fsxs show\" / \"fsxs --var\":\n";
  74. print "\tcflags ldflags libs solink includes cc ld mkdir install\n";
  75. print "\tprefix libdir modulesdir dbdir includedir confdir bindir\n";
  76. print "\n";
  77. print "Examples:\n";
  78. print "\t$0 compile --add-cflags=\"-DFOO=1 -DBAR\" mod_test.c mod_test2.c\n\n";
  79. print "\t$0 link --add-ldflags=\"-ltest\" mod_test.so mod_test.o mod_test2.o\n\n";
  80. print "\t$0 build --add-cflags=\"-DFOO\" --add-ldflags=\"-ltest\" mod_test.so mod_test.c mod_test2.c\n\n";
  81. exit 1;
  82. }
  83. sub fsxs_compile {
  84. my $cc_cmd;
  85. $cc_cmd = "$vars{CC}";
  86. if( defined( $vars{INCLUDES} ) && $vars{INCLUDES} ) {
  87. $cc_cmd = $cc_cmd . " $vars{INCLUDES}"
  88. }
  89. $cc_cmd = $cc_cmd . " $vars{CFLAGS} -c -o";
  90. foreach( @_ ) {
  91. chomp( $_ );
  92. # replace file extension
  93. my $outfile = $_;
  94. $outfile =~ s/\.(cpp|cc|c)$/.o/;
  95. print "CC\t$_\n";
  96. if( $debug ) {
  97. print "$cc_cmd $outfile $_\n"
  98. }
  99. do_exec( "$cc_cmd $outfile $_" );
  100. }
  101. }
  102. sub fsxs_link {
  103. my $target = shift;
  104. my @objs = @_;
  105. my $ld_cmd;
  106. $ld_cmd = "$vars{LD}";
  107. $ld_cmd = $ld_cmd . " $vars{LDFLAGS} $vars{SOLINK} -o";
  108. print "LD\t$target\t[@objs]\n";
  109. if( $debug ) {
  110. print "$ld_cmd $target @objs $vars{LIBS}\n"
  111. }
  112. do_exec( "$ld_cmd $target @objs $vars{LIBS}" );
  113. }
  114. sub fsxs_install {
  115. my @files = @_;
  116. my $destination = $vars{DESTDIR} . $vars{MODULESDIR};
  117. # check if destination exists, create if it doesn't
  118. if( ! -e $destination ) {
  119. if( $debug ) {
  120. print "$vars{MKDIR} $destination\n";
  121. }
  122. do_exec( "$vars{MKDIR} $destination" );
  123. }
  124. if( $debug ) {
  125. print "$vars{INSTALL} -m644 @files $destination\n";
  126. }
  127. do_exec( "$vars{INSTALL} -m644 @files $destination" );
  128. }
  129. sub fsxs_show {
  130. my @varlist = @_;
  131. if( $#varlist < 0 ) {
  132. # none requested, show all variables with names
  133. my $key;
  134. foreach $key ( keys %vars ) {
  135. print "$key: $vars{$key}\n";
  136. }
  137. }
  138. elsif( $#varlist > 0 ) {
  139. # more than one requested, show with name
  140. foreach( @varlist ) {
  141. if( defined $vars{$_} ) {
  142. print "$_: $vars{$_}\n";
  143. }
  144. }
  145. } else {
  146. # show only one variable, without name
  147. if( defined $vars{$varlist[0]} ) {
  148. print "$vars{$varlist[0]}\n";
  149. }
  150. }
  151. }
  152. sub fsxs_showq {
  153. my @varlist = @_;
  154. my $count = 0;
  155. if( $#varlist >= 0 ) {
  156. foreach( @varlist ) {
  157. if( defined $vars{$_} ) {
  158. print "$vars{$_}" . (($count < $#varlist) ? " " : "");
  159. }
  160. $count++;
  161. }
  162. }
  163. }
  164. #
  165. # main part
  166. #
  167. if( $#ARGV < 0 ) {
  168. fsxs_usage;
  169. }
  170. if( @ARGV[0] =~ /^\--.+$/ ) {
  171. # 'show' shortcut for using fsxs in scripts
  172. $needs_target = 0;
  173. $command = "showq";
  174. }
  175. else {
  176. chomp( $command = shift @ARGV );
  177. if( $command =~ /^install|build|link|compile|show$/ ) {
  178. # add -lfreeswitch to the front of the library list
  179. # we don't want it to be in the show / showq output
  180. # but we still want to be able to override it with --set-libs
  181. if( $command ne "show" ) {
  182. $vars{LIBS} = "-lfreeswitch $vars{LIBS}";
  183. }
  184. if( $command =~ /^show|compile|install$/ ) {
  185. $needs_target = 0;
  186. }
  187. }
  188. else {
  189. print STDERR "Unknown command: $command\n";
  190. fsxs_usage;
  191. }
  192. }
  193. # parse environment variables
  194. if( defined $ENV{DEBUG} && $ENV{DEBUG} ) {
  195. $debug = 1;
  196. }
  197. # parse arguments
  198. foreach(@ARGV) {
  199. chomp( $_ );
  200. if( $command ne "show" && $command ne "showq" )
  201. {
  202. if( /^\--add-cflags=(.*)$/ ) {
  203. $vars{CFLAGS} = "$vars{CFLAGS} $1";
  204. }
  205. elsif( /^\--set-cflags=(.*)$/ ) {
  206. $vars{CFLAGS} = "$1";
  207. }
  208. elsif( /^\--add-ldflags=(.*)$/ ) {
  209. $vars{LDFLAGS} = "$vars{LDFLAGS} $1";
  210. }
  211. elsif( /^\--set-ldflags=(.*)$/ ) {
  212. $vars{LDFLAGS} = "$1";
  213. }
  214. elsif( /^\--add-libs=(.*)$/ ) {
  215. $vars{LIBS} = "$vars{LIBS} $1";
  216. }
  217. elsif( /^\--set-libs=(.*)$/ ) {
  218. $vars{LIBS} = "$1";
  219. }
  220. elsif( /^\--destdir=(.*)$/ ) {
  221. $vars{DESTDIR} = "$1";
  222. }
  223. elsif( /^\--debug$/ ) {
  224. $debug = 1;
  225. }
  226. elsif( /^(DESTDIR|CFLAGS|CC|LDFLAGS|LD|LIBS)=(.*)$/ ) {
  227. if( $debug ) {
  228. print "Overriding $1 (new value: $2)\n";
  229. }
  230. $vars{$1} = "$2";
  231. }
  232. elsif( /^([^\-]+.*)$/ ) {
  233. if( $needs_target ) {
  234. $target = "$1";
  235. $needs_target = 0;
  236. } else {
  237. push(@files, "$1");
  238. }
  239. }
  240. } else {
  241. # show command has different parameter handling
  242. if( /^\--(.*)$/ ) {
  243. push( @files, uc "$1" );
  244. }
  245. elsif( /^([^\-]+.*)$/ ) {
  246. push( @files, uc "$1" );
  247. }
  248. }
  249. }
  250. #
  251. # execute commands
  252. #
  253. if( $command eq 'link' ) {
  254. fsxs_link( $target, @files );
  255. }
  256. elsif( $command eq 'compile' ) {
  257. fsxs_compile( @files );
  258. }
  259. elsif( $command eq 'build' ) {
  260. my @objs = ();
  261. fsxs_compile( @files );
  262. foreach( @files ) {
  263. chomp( $_ );
  264. $_ =~ s/\.(cpp|cc|c)$/.o/;
  265. push( @objs, "$_" );
  266. }
  267. fsxs_link( $target, @objs );
  268. }
  269. elsif( $command eq 'show' ) {
  270. fsxs_show( @files );
  271. }
  272. elsif( $command eq 'showq' ) {
  273. fsxs_showq( @files );
  274. }
  275. elsif( $command eq 'install' ) {
  276. fsxs_install( @files );
  277. }
  278. else {
  279. fsxs_usage;
  280. }
  281. exit 0;