summaryrefslogtreecommitdiff
path: root/src/main.cc
diff options
context:
space:
mode:
authorAdrian Thurston <thurston@colm.net>2021-12-04 09:48:13 -0800
committerGitHub <noreply@github.com>2021-12-04 09:48:13 -0800
commit0856b1a230400fc4358ef00fa422db986f685555 (patch)
tree720500f71252e0f92ca713512bdaf1c673db0fa9 /src/main.cc
parentc77aceb85139404a602fbd463f631235f99773d9 (diff)
parent7b2524ec0c036a3aab8e97e77c8a94b3dc12fe82 (diff)
downloadcolm-0856b1a230400fc4358ef00fa422db986f685555.tar.gz
Merge pull request #138 from viccie30/fix-out-of-tree-build
Some build system fixes
Diffstat (limited to 'src/main.cc')
-rw-r--r--src/main.cc124
1 files changed, 35 insertions, 89 deletions
diff --git a/src/main.cc b/src/main.cc
index 8ca395f0..06647497 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -78,6 +78,7 @@ const char *binaryFn = 0;
const char *exportHeaderFn = 0;
const char *exportCodeFn = 0;
const char *commitCodeFn = 0;
+const char *buildDir = 0;
const char *objectName = "colm_object";
bool exportCode = false;
bool hostAdapters = true;
@@ -441,67 +442,49 @@ void runOutputProgram()
/* We shall never return here! */
}
-void compileOutput( const char *argv0, const bool inSource, char *srcLocation )
+void compileOutput()
{
- /* Find the location of the colm program that is executing. */
- char *location = strdup( argv0 );
- char *last;
- int length = 1024 + strlen( intermedFn ) + strlen( binaryFn );
- if ( inSource ) {
- last = strrchr( location, '/' );
- assert( last != 0 );
- last[0] = 0;
- length += 3 * strlen( location );
- }
- else {
- last = location + strlen( location ) - 1;
- while ( true ) {
- if ( last == location ) {
- last[0] = '.';
- last[1] = 0;
- break;
- }
- if ( *last == '/' ) {
- last[0] = 0;
- break;
- }
- last -= 1;
- }
- }
+ const char *compiler = getenv( "CC" );
+ if ( compiler == 0 )
+ compiler = "gcc";
+
+ const char *cflags = getenv( "CFLAGS" );
+ if ( cflags == 0 )
+ cflags = "";
+
+ int length = 1024 + strlen( compiler ) + strlen( cflags ) +
+ strlen( intermedFn ) + strlen( binaryFn );
for ( ArgsVector::Iter af = additionalCodeFiles; af.lte(); af++ )
length += strlen( *af ) + 2;
for ( ArgsVector::Iter ip = includePaths; ip.lte(); ip++ )
length += strlen( *ip ) + 3;
for ( ArgsVector::Iter lp = libraryPaths; lp.lte(); lp++ )
length += strlen( *lp ) + 3;
-#define COMPILE_COMMAND_STRING "gcc -Wall -Wwrite-strings" \
- " -g" \
+ if ( buildDir != 0 )
+ length += strlen( buildDir ) * 3;
+#define COMPILE_COMMAND_STRING "%s -Wall -Wwrite-strings" \
+ " -g %s" \
" -o %s" \
" %s"
char *command = new char[length];
- if ( inSource ) {
+ if ( buildDir != 0 ) {
sprintf( command,
+ "%s/libtool --tag=CC --mode=link "
COMPILE_COMMAND_STRING
- " -I%s/../aapl"
- " -I%s/include"
- " -L%s"
-#if defined(LINK_STATIC)
- " %s/libcolm.a",
-#elif defined(LINK_SHARED)
- " %s/libcolm.so",
-#else
-# error "must enabled at least one of shared or static libs"
-#endif
-
- binaryFn, intermedFn, srcLocation,
- srcLocation, location, location );
+ " -I%s/src/include"
+ " -static"
+ " %s/src/libcolm.la",
+ buildDir, compiler, cflags,
+ binaryFn, intermedFn,
+ buildDir, buildDir );
}
else {
sprintf( command,
COMPILE_COMMAND_STRING
- " -I" PREFIX "/include"
- " -L" PREFIX "/lib"
- " -Wl,-rpath," PREFIX "/lib",
+ " -I" INCLUDEDIR
+ " -L" LIBDIR
+ " -Wl,-rpath," LIBDIR,
+ compiler, cflags,
binaryFn, intermedFn );
}
#undef COMPILE_COMMAND_STRING
@@ -518,7 +501,7 @@ void compileOutput( const char *argv0, const bool inSource, char *srcLocation )
strcat( command, *lp );
}
- if ( !inSource )
+ if ( buildDir == 0 )
strcat( command, " -lcolm" );
if( !compileOutputCommand( command ) && run )
@@ -527,46 +510,9 @@ void compileOutput( const char *argv0, const bool inSource, char *srcLocation )
delete[] command;
}
-bool inSourceTree( const char *argv0, char *&location )
-{
- const char *lastSlash = strrchr( argv0, '/' );
- if ( lastSlash != 0 ) {
- /* Take off the file name. */
- int rootLen = lastSlash - argv0;
-
- /* Create string for dir. */
- char *mainPath = new char[rootLen + 16];
- memcpy( mainPath, argv0, rootLen );
- mainPath[rootLen] = 0;
-
- /* If built using ldconfig then there will be a .libs dir. */
- lastSlash = strrchr( mainPath, '/' );
- if ( lastSlash != 0 ) {
- if ( strlen( lastSlash ) >= 6 && memcmp( lastSlash, "/.libs", 7 ) == 0 ) {
- rootLen = lastSlash - mainPath;
- mainPath[rootLen] = 0;
- }
- }
-
- strcpy( mainPath + rootLen, "/main.cc" );
-
- struct stat sb;
- int res = stat( mainPath, &sb );
- if ( res == 0 && S_ISREG( sb.st_mode ) ) {
- mainPath[rootLen] = 0;
- location = mainPath;
- return true;
- }
-
- delete[] mainPath;
- }
-
- return false;
-}
-
void processArgs( int argc, const char **argv )
{
- ParamCheck pc( "p:cD:e:x:I:L:vdliro:S:M:vHh?-:sVa:m:b:E:", argc, argv );
+ ParamCheck pc( "p:cD:e:x:I:L:vdliro:S:M:vHh?-:sVa:m:b:E:B:", argc, argv );
while ( pc.check() ) {
switch ( pc.state ) {
@@ -658,6 +604,9 @@ void processArgs( int argc, const char **argv )
case 'm':
commitCodeFn = pc.parameterArg;
break;
+ case 'B':
+ buildDir = pc.parameterArg;
+ break;
case 'E': {
const char *eq = strchr( pc.parameterArg, '=' );
@@ -811,11 +760,8 @@ int main(int argc, const char **argv)
if ( outStream != 0 )
delete outStream;
- if ( !gblLibrary ) {
- char *location = 0;
- bool inSource = inSourceTree( argv[0], location );
- compileOutput( argv[0], inSource, location );
- }
+ if ( !gblLibrary )
+ compileOutput();
if ( exportHeaderFn != 0 ) {
openExports();