Changed file type and search path handling
git-svn-id: svn://svn.cc65.org/cc65/trunk@2185 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
152
src/ld65/main.c
152
src/ld65/main.c
@@ -40,6 +40,7 @@
|
|||||||
|
|
||||||
/* common */
|
/* common */
|
||||||
#include "cmdline.h"
|
#include "cmdline.h"
|
||||||
|
#include "filetype.h"
|
||||||
#include "libdefs.h"
|
#include "libdefs.h"
|
||||||
#include "objdefs.h"
|
#include "objdefs.h"
|
||||||
#include "print.h"
|
#include "print.h"
|
||||||
@@ -73,8 +74,6 @@
|
|||||||
|
|
||||||
static unsigned ObjFiles = 0; /* Count of object files linked */
|
static unsigned ObjFiles = 0; /* Count of object files linked */
|
||||||
static unsigned LibFiles = 0; /* Count of library files linked */
|
static unsigned LibFiles = 0; /* Count of library files linked */
|
||||||
static const char* LibPath = 0; /* Search path for modules */
|
|
||||||
static unsigned LibPathLen = 0; /* Length of LibPath */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -108,9 +107,11 @@ static void Usage (void)
|
|||||||
" --config name\t\tUse linker config file\n"
|
" --config name\t\tUse linker config file\n"
|
||||||
" --dump-config name\tDump a builtin configuration\n"
|
" --dump-config name\tDump a builtin configuration\n"
|
||||||
" --help\t\tHelp (this text)\n"
|
" --help\t\tHelp (this text)\n"
|
||||||
|
" --lib file\t\tLink this library\n"
|
||||||
" --lib-path path\tSpecify a library search path\n"
|
" --lib-path path\tSpecify a library search path\n"
|
||||||
" --mapfile name\tCreate a map file\n"
|
" --mapfile name\tCreate a map file\n"
|
||||||
" --module-id id\tSpecify a module id\n"
|
" --module-id id\tSpecify a module id\n"
|
||||||
|
" --obj file\t\tLink this object file\n"
|
||||||
" --obj-path path\tSpecify an object file search path\n"
|
" --obj-path path\tSpecify an object file search path\n"
|
||||||
" --start-addr addr\tSet the default start address\n"
|
" --start-addr addr\tSet the default start address\n"
|
||||||
" --target sys\t\tSet the target system\n"
|
" --target sys\t\tSet the target system\n"
|
||||||
@@ -147,78 +148,75 @@ static unsigned long CvtNumber (const char* Arg, const char* Number)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
static int HasPath (const char* Name)
|
static void LinkFile (const char* Name, FILETYPE Type)
|
||||||
/* Check if the given Name has a path component */
|
|
||||||
{
|
|
||||||
return strchr (Name, '/') != 0 || strchr (Name, '\\') != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void LinkFile (const char* Name)
|
|
||||||
/* Handle one file */
|
/* Handle one file */
|
||||||
{
|
{
|
||||||
|
char* PathName;
|
||||||
|
FILE* F;
|
||||||
unsigned long Magic;
|
unsigned long Magic;
|
||||||
unsigned Len;
|
|
||||||
char* NewName = 0;
|
|
||||||
|
/* If we don't know the file type, determine it from the extension */
|
||||||
|
if (Type == FILETYPE_UNKNOWN) {
|
||||||
|
Type = GetFileType (Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* For known file types, search the file in the directory list */
|
||||||
|
switch (Type) {
|
||||||
|
|
||||||
|
case FILETYPE_LIB:
|
||||||
|
PathName = SearchFile (Name, SEARCH_LIB);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case FILETYPE_OBJ:
|
||||||
|
PathName = SearchFile (Name, SEARCH_OBJ);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
PathName = xstrdup (Name); /* Use the name as is */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We must have a valid name now */
|
||||||
|
if (PathName == 0) {
|
||||||
|
Error ("Input file `%s' not found", Name);
|
||||||
|
}
|
||||||
|
|
||||||
/* Try to open the file */
|
/* Try to open the file */
|
||||||
FILE* F = fopen (Name, "rb");
|
F = fopen (PathName, "rb");
|
||||||
if (F == 0) {
|
if (F == 0) {
|
||||||
/* We couldn't open the file. If the name doesn't have a path, and we
|
Error ("Cannot open `%s': %s", PathName, strerror (errno));
|
||||||
* have a search path given, try the name with the search path
|
|
||||||
* prepended.
|
|
||||||
*/
|
|
||||||
if (LibPathLen > 0 && !HasPath (Name)) {
|
|
||||||
/* Allocate memory. Account for the trailing zero, and for a
|
|
||||||
* path separator character eventually needed.
|
|
||||||
*/
|
|
||||||
Len = LibPathLen;
|
|
||||||
NewName = xmalloc (strlen (Name) + Len + 2);
|
|
||||||
/* Build the new name */
|
|
||||||
memcpy (NewName, LibPath, Len);
|
|
||||||
if (NewName [Len-1] != '/' && NewName [Len-1] != '\\') {
|
|
||||||
/* We need an additional path separator */
|
|
||||||
NewName [Len++] = '/';
|
|
||||||
}
|
|
||||||
strcpy (NewName + Len, Name);
|
|
||||||
|
|
||||||
/* Now try to open the new file */
|
|
||||||
F = fopen (NewName, "rb");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (F == 0) {
|
|
||||||
Error ("Cannot open `%s': %s", Name, strerror (errno));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read the magic word */
|
/* Read the magic word */
|
||||||
Magic = Read32 (F);
|
Magic = Read32 (F);
|
||||||
|
|
||||||
/* Do we know this type of file? */
|
/* Check the magic for known file types. The handling is somewhat weird
|
||||||
|
* since we may have given a file with a ".lib" extension, which was
|
||||||
|
* searched and found in a directory for library files, but we now find
|
||||||
|
* out (by looking at the magic) that it's indeed an object file. We just
|
||||||
|
* ignore the problem and hope no one will notice...
|
||||||
|
*/
|
||||||
switch (Magic) {
|
switch (Magic) {
|
||||||
|
|
||||||
case OBJ_MAGIC:
|
case OBJ_MAGIC:
|
||||||
ObjAdd (F, Name);
|
ObjAdd (F, PathName);
|
||||||
++ObjFiles;
|
++ObjFiles;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LIB_MAGIC:
|
case LIB_MAGIC:
|
||||||
LibAdd (F, Name);
|
LibAdd (F, PathName);
|
||||||
++LibFiles;
|
++LibFiles;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fclose (F);
|
fclose (F);
|
||||||
Error ("File `%s' has unknown type", Name);
|
Error ("File `%s' has unknown type", PathName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we have allocated memory, free it here. Note: Memory will not always
|
/* Free allocated memory. */
|
||||||
* be freed if we run into an error, but that's no problem. Adding more
|
xfree (PathName);
|
||||||
* code to work around it will use more memory than the chunk that's lost.
|
|
||||||
*/
|
|
||||||
xfree (NewName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -234,10 +232,19 @@ static void OptCfgPath (const char* Opt attribute ((unused)), const char* Arg)
|
|||||||
static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
|
static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
|
||||||
/* Define the config file */
|
/* Define the config file */
|
||||||
{
|
{
|
||||||
|
char* PathName;
|
||||||
|
|
||||||
if (CfgAvail ()) {
|
if (CfgAvail ()) {
|
||||||
Error ("Cannot use -C/-t twice");
|
Error ("Cannot use -C/-t twice");
|
||||||
}
|
}
|
||||||
CfgSetName (Arg);
|
/* Search for the file */
|
||||||
|
PathName = SearchFile (Arg, SEARCH_CFG);
|
||||||
|
if (PathName == 0) {
|
||||||
|
Error ("Cannot find config file `%s'", Arg);
|
||||||
|
} else {
|
||||||
|
CfgSetName (PathName);
|
||||||
|
xfree (PathName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -275,6 +282,14 @@ static void OptHelp (const char* Opt attribute ((unused)),
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void OptLib (const char* Opt attribute ((unused)), const char* Arg)
|
||||||
|
/* Link a library */
|
||||||
|
{
|
||||||
|
LinkFile (Arg, FILETYPE_LIB);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
|
static void OptLibPath (const char* Opt attribute ((unused)), const char* Arg)
|
||||||
/* Specify a library file search path */
|
/* Specify a library file search path */
|
||||||
{
|
{
|
||||||
@@ -303,6 +318,14 @@ static void OptModuleId (const char* Opt, const char* Arg)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void OptObj (const char* Opt attribute ((unused)), const char* Arg)
|
||||||
|
/* Link an object file */
|
||||||
|
{
|
||||||
|
LinkFile (Arg, FILETYPE_OBJ);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
|
static void OptObjPath (const char* Opt attribute ((unused)), const char* Arg)
|
||||||
/* Specify an object file search path */
|
/* Specify an object file search path */
|
||||||
{
|
{
|
||||||
@@ -362,9 +385,11 @@ int main (int argc, char* argv [])
|
|||||||
{ "--dbgfile", 1, OptDbgFile },
|
{ "--dbgfile", 1, OptDbgFile },
|
||||||
{ "--dump-config", 1, OptDumpConfig },
|
{ "--dump-config", 1, OptDumpConfig },
|
||||||
{ "--help", 0, OptHelp },
|
{ "--help", 0, OptHelp },
|
||||||
|
{ "--lib", 1, OptLib },
|
||||||
{ "--lib-path", 1, OptLibPath },
|
{ "--lib-path", 1, OptLibPath },
|
||||||
{ "--mapfile", 1, OptMapFile },
|
{ "--mapfile", 1, OptMapFile },
|
||||||
{ "--module-id", 1, OptModuleId },
|
{ "--module-id", 1, OptModuleId },
|
||||||
|
{ "--obj", 1, OptObj },
|
||||||
{ "--obj-path", 1, OptObjPath },
|
{ "--obj-path", 1, OptObjPath },
|
||||||
{ "--start-addr", 1, OptStartAddr },
|
{ "--start-addr", 1, OptStartAddr },
|
||||||
{ "--target", 1, OptTarget },
|
{ "--target", 1, OptTarget },
|
||||||
@@ -376,17 +401,8 @@ int main (int argc, char* argv [])
|
|||||||
/* Initialize the cmdline module */
|
/* Initialize the cmdline module */
|
||||||
InitCmdLine (&argc, &argv, "ld65");
|
InitCmdLine (&argc, &argv, "ld65");
|
||||||
|
|
||||||
/* Evaluate the CC65_LIB environment variable */
|
/* Initialize the input file search paths */
|
||||||
LibPath = getenv ("CC65_LIB");
|
InitSearchPaths ();
|
||||||
if (LibPath == 0) {
|
|
||||||
/* Use some default path */
|
|
||||||
#ifdef CC65_LIB
|
|
||||||
LibPath = CC65_LIB;
|
|
||||||
#else
|
|
||||||
LibPath = "/usr/lib/cc65/lib/";
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
LibPathLen = strlen (LibPath);
|
|
||||||
|
|
||||||
/* Check the parameters */
|
/* Check the parameters */
|
||||||
I = 1;
|
I = 1;
|
||||||
@@ -462,7 +478,7 @@ int main (int argc, char* argv [])
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* A filename */
|
/* A filename */
|
||||||
LinkFile (Arg);
|
LinkFile (Arg, FILETYPE_UNKNOWN);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
/* common */
|
/* common */
|
||||||
|
#include "fname.h"
|
||||||
#include "xmalloc.h"
|
#include "xmalloc.h"
|
||||||
|
|
||||||
/* ld65 */
|
/* ld65 */
|
||||||
@@ -65,14 +66,7 @@ static const char* GetModule (const char* Name)
|
|||||||
/* Get a module name from the file name */
|
/* Get a module name from the file name */
|
||||||
{
|
{
|
||||||
/* Make a module name from the file name */
|
/* Make a module name from the file name */
|
||||||
const char* Module = Name + strlen (Name);
|
const char* Module = FindName (Name);
|
||||||
while (Module > Name) {
|
|
||||||
--Module;
|
|
||||||
if (*Module == '/' || *Module == '\\') {
|
|
||||||
++Module;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (*Module == 0) {
|
if (*Module == 0) {
|
||||||
Error ("Cannot make module name from `%s'", Name);
|
Error ("Cannot make module name from `%s'", Name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user