aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/msp430
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/config/msp430')
-rw-r--r--gcc/config/msp430/driver-msp430.c13
-rw-r--r--gcc/config/msp430/msp430-devices.c133
-rw-r--r--gcc/config/msp430/msp430-devices.h2
-rw-r--r--gcc/config/msp430/msp430.c23
-rw-r--r--gcc/config/msp430/msp430.h12
5 files changed, 178 insertions, 5 deletions
diff --git a/gcc/config/msp430/driver-msp430.c b/gcc/config/msp430/driver-msp430.c
index c37b169..fbe7c44 100644
--- a/gcc/config/msp430/driver-msp430.c
+++ b/gcc/config/msp430/driver-msp430.c
@@ -150,6 +150,19 @@ msp430_select_hwmult_lib (int argc ATTRIBUTE_UNUSED,
return "-lmul_none";
}
+/* Spec function. Used to place the path to the MSP430-GCC support files
+ on the command line, prefixed with "-L", so the linker finds the linker
+ scripts in that directory. */
+const char *
+msp430_get_linker_devices_include_path (int argc ATTRIBUTE_UNUSED,
+ const char **argv ATTRIBUTE_UNUSED)
+{
+ char *devices_csv_path;
+ if (msp430_check_env_var_for_devices (&devices_csv_path))
+ return NULL;
+ return concat ("-L", msp430_dirname (devices_csv_path), NULL);
+}
+
/* Spec function. Propagate -m{code,data}-region= to the linker, unless the
lower region has been specified without -muse-lower-region-prefix also being
used. */
diff --git a/gcc/config/msp430/msp430-devices.c b/gcc/config/msp430/msp430-devices.c
index 537d438..600a111 100644
--- a/gcc/config/msp430/msp430-devices.c
+++ b/gcc/config/msp430/msp430-devices.c
@@ -37,7 +37,7 @@ extern struct t_msp430_mcu_data hard_msp430_mcu_data[605];
/* Set to the full path to devices.csv if it is found by searching the -I and
-L paths. */
-char * derived_devices_csv_loc = NULL;
+char *derived_devices_csv_loc = NULL;
/* This is to canonicalize the directory separators in the path.
On Windows we could have a mix of '/' and '\' in the path. */
@@ -52,6 +52,128 @@ canonicalize_path_dirsep (char **path)
t_path[i] = DIR_SEPARATOR;
}
+/* This function returns the enclosing directory of PATH.
+ It is inconsequential whether PATH ends in a dirsep or not.
+ It modifies the string pointed to by PATH. */
+char *
+msp430_dirname (char *path)
+{
+ int last_elem = strlen (path) - 1;
+ int i = last_elem - (IS_DIR_SEPARATOR (path[last_elem]) ? 1 : 0);
+ for (; i >= 0; i--)
+ {
+ if (IS_DIR_SEPARATOR (path[i]))
+ {
+ path[i] = '\0';
+ return path;
+ }
+ }
+ return path;
+}
+
+/* devices.csv path from the toolchain root. */
+static const char rest_of_devices_path[] = "/msp430-elf/include/devices/";
+
+/* "The default value of GCC_EXEC_PREFIX is prefix/lib/gcc". Strip lib/gcc
+ from GCC_EXEC_PREFIX to get the path to the installed toolchain. */
+static void
+extract_devices_dir_from_exec_prefix (char **devices_loc)
+{
+ const char *temp;
+ char *gcc_exec_prefix = *devices_loc;
+ int len = strlen (gcc_exec_prefix);
+
+ /* Copied from gcc.c. */
+ if (len > (int) sizeof ("/lib/gcc/") - 1
+ && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
+ {
+ temp = gcc_exec_prefix + len - sizeof ("/lib/gcc/") + 1;
+ if (IS_DIR_SEPARATOR (*temp)
+ && filename_ncmp (temp + 1, "lib", 3) == 0
+ && IS_DIR_SEPARATOR (temp[4])
+ && filename_ncmp (temp + 5, "gcc", 3) == 0)
+ {
+ len -= sizeof ("/lib/gcc/") - 1;
+ /* Keep the '/' from the beginning of /lib/gcc. */
+ gcc_exec_prefix[len + 1] = (char) 0;
+ *devices_loc = concat (gcc_exec_prefix, rest_of_devices_path, NULL);
+ return;
+ }
+ }
+}
+
+/* Given the path to the GCC executable, return the path to the installed
+ device data in "$TOOLCHAIN_ROOT/msp430-elf/include/devices".
+ Assumes the GCC executable is in "$TOOLCHAIN_ROOT/<somedir>/". */
+static void
+extract_devices_dir_from_collect_gcc (char **devices_loc)
+{
+ char *t_devices_loc = *devices_loc;
+ /* Go up a directory to the toolchain root. */
+ t_devices_loc = msp430_dirname (msp430_dirname (t_devices_loc));
+ t_devices_loc = concat (t_devices_loc, rest_of_devices_path, NULL);
+ *devices_loc = t_devices_loc;
+}
+
+/* The path to the MSP430-GCC support files can be specified with the
+ environment variable "MSP430_GCC_INCLUDE_DIR", or installed into the
+ toolchain in the msp430-elf/include/devices subdirectory.
+ We use the GCC_EXEC_PREFIX or COLLECT_GCC environment variables as a starting
+ point for the location of the toolchain, and work out the path to the
+ installed device data from there.
+ Return 0 and set LOCAL_DEVICES_CSV_LOC if we find devices.csv. Return 1
+ if devices.csv wasn't found. */
+int
+msp430_check_env_var_for_devices (char **local_devices_csv_loc)
+{
+ const int num_vars = 3;
+ const char dirsep[2] = { DIR_SEPARATOR, 0 };
+ /* Both GCC_EXEC_PREFIX and COLLECT_GCC should always be set to the format we
+ expect, as they are required for correct operation of the toolchain.
+ So if they are wrong the user will probably have bigger problems.
+ GCC_EXEC_PREFIX is only defined in the driver, whilst COLLECT_GCC is only
+ defined in the compiler proper, so we need both. */
+ const char *env_vars[num_vars] = {
+ "MSP430_GCC_INCLUDE_DIR", "GCC_EXEC_PREFIX", "COLLECT_GCC" };
+ enum msp430_include_vars {
+ MSP430_GCC_INCLUDE_DIR,
+ GCC_EXEC_PREFIX,
+ COLLECT_GCC
+ };
+ FILE *devices_csv_file = NULL;
+ int i;
+
+ for (i = MSP430_GCC_INCLUDE_DIR; i <= COLLECT_GCC; i++)
+ {
+ char *t_devices_loc;
+ char *val = getenv (env_vars[i]);
+ if (val == NULL)
+ continue;
+ t_devices_loc = xstrdup (val);
+
+ if (i == MSP430_GCC_INCLUDE_DIR)
+ {
+ if (!IS_DIR_SEPARATOR (t_devices_loc[strlen (t_devices_loc) - 1]))
+ t_devices_loc = concat (t_devices_loc, dirsep, NULL);
+ }
+ else if (i == GCC_EXEC_PREFIX)
+ extract_devices_dir_from_exec_prefix (&t_devices_loc);
+ else if (i == COLLECT_GCC)
+ extract_devices_dir_from_collect_gcc (&t_devices_loc);
+
+ t_devices_loc = concat (t_devices_loc, "devices.csv", NULL);
+ devices_csv_file = fopen (t_devices_loc, "r");
+ if (devices_csv_file != NULL)
+ {
+ fclose (devices_csv_file);
+ *local_devices_csv_loc = t_devices_loc;
+ canonicalize_path_dirsep (local_devices_csv_loc);
+ return 0;
+ }
+ }
+ return 1;
+}
+
/* Spec function which searches the paths passed to the -I and -L options for
the "devices.csv" file. If it is found then the -mdevices-csv-loc option is
placed on the command line so the compiler knows the location of the
@@ -69,7 +191,7 @@ msp430_check_path_for_devices (int argc, const char **argv)
return NULL;
for (i = 0; i < argc; i++)
{
- char *inc_path = ASTRDUP (argv[i]);
+ char *inc_path = xstrdup (argv[i]);
canonicalize_path_dirsep (&inc_path);
if (!IS_DIR_SEPARATOR (inc_path[strlen (inc_path) - 1]))
inc_path = concat (inc_path, dirsep, NULL);
@@ -274,8 +396,11 @@ parse_devices_csv (const char * mcu_name)
/* Otherwise check if the path to devices.csv was found another way. */
else if (derived_devices_csv_loc != NULL)
return parse_devices_csv_1 (derived_devices_csv_loc, mcu_name);
- /* devices.csv was not found. */
- return 2;
+ /* Otherwise we need to use environment variables to try and find it. */
+ if (msp430_check_env_var_for_devices (&derived_devices_csv_loc))
+ /* devices.csv was not found. */
+ return 2;
+ return parse_devices_csv_1 (derived_devices_csv_loc, mcu_name);
}
/* Main entry point to load the MCU data for the given -mmcu into
diff --git a/gcc/config/msp430/msp430-devices.h b/gcc/config/msp430/msp430-devices.h
index 08ae1ad..9e8029a 100644
--- a/gcc/config/msp430/msp430-devices.h
+++ b/gcc/config/msp430/msp430-devices.h
@@ -29,3 +29,5 @@ struct t_msp430_mcu_data
extern struct t_msp430_mcu_data extracted_mcu_data;
void msp430_extract_mcu_data (const char * mcu_name);
+int msp430_check_env_var_for_devices (char **local_devices_csv_loc);
+char *msp430_dirname (char *path);
diff --git a/gcc/config/msp430/msp430.c b/gcc/config/msp430/msp430.c
index c058690..b5d3edb 100644
--- a/gcc/config/msp430/msp430.c
+++ b/gcc/config/msp430/msp430.c
@@ -47,6 +47,8 @@
#include "builtins.h"
#include "intl.h"
#include "msp430-devices.h"
+#include "incpath.h"
+#include "prefix.h"
/* This file should be included last. */
#include "target-def.h"
@@ -3634,6 +3636,27 @@ msp430_incoming_return_addr_rtx (void)
{
return gen_rtx_MEM (Pmode, stack_pointer_rtx);
}
+
+/* If the path to the MSP430-GCC support files has been found by examining
+ an environment variable (see msp430_check_env_var_for_devices in
+ msp430-devices.c), or -mdevices-csv-loc=, register this path as an include
+ directory so the user can #include msp430.h without needing to specify the
+ path to the support files with -I. */
+void
+msp430_register_pre_includes (const char *sysroot ATTRIBUTE_UNUSED,
+ const char *iprefix ATTRIBUTE_UNUSED,
+ int stdinc ATTRIBUTE_UNUSED)
+{
+ char *include_dir;
+ if (msp430_devices_csv_loc)
+ include_dir = xstrdup (msp430_devices_csv_loc);
+ else if (msp430_check_env_var_for_devices (&include_dir))
+ return;
+ include_dir = msp430_dirname (include_dir);
+
+ include_dir = update_path (include_dir, "");
+ add_path (include_dir, INC_SYSTEM, false, false);
+}
/* Instruction generation stuff. */
diff --git a/gcc/config/msp430/msp430.h b/gcc/config/msp430/msp430.h
index 73afe2e..2e70e0d 100644
--- a/gcc/config/msp430/msp430.h
+++ b/gcc/config/msp430/msp430.h
@@ -75,6 +75,7 @@ extern bool msp430x;
"msp430_propagate_region_opt(%* %{muse-lower-region-prefix})} " \
"%{mdata-region=*:--data-region=%:" \
"msp430_propagate_region_opt(%* %{muse-lower-region-prefix})} " \
+ "%:msp430_get_linker_devices_include_path()"
#define DRIVER_SELF_SPECS \
" %{!mlarge:%{mcode-region=*:%{mdata-region=*:%e-mcode-region and " \
@@ -94,6 +95,7 @@ extern const char * msp430_select_cpu (int, const char **);
extern const char * msp430_set_driver_var (int, const char **);
extern const char * msp430_check_path_for_devices (int, const char **);
extern const char *msp430_propagate_region_opt (int, const char **);
+extern const char *msp430_get_linker_devices_include_path (int, const char **);
/* There must be a trailing comma after the last item, see gcc.c
"static_spec_functions". */
@@ -102,7 +104,9 @@ extern const char *msp430_propagate_region_opt (int, const char **);
{ "msp430_select_cpu", msp430_select_cpu }, \
{ "msp430_set_driver_var", msp430_set_driver_var }, \
{ "msp430_check_path_for_devices", msp430_check_path_for_devices }, \
- { "msp430_propagate_region_opt", msp430_propagate_region_opt },
+ { "msp430_propagate_region_opt", msp430_propagate_region_opt }, \
+ { "msp430_get_linker_devices_include_path", \
+ msp430_get_linker_devices_include_path },
/* Specify the libraries to include on the linker command line.
@@ -496,6 +500,12 @@ typedef struct
#define TARGET_HAS_NO_HW_DIVIDE (! TARGET_HWMULT)
+void msp430_register_pre_includes (const char *sysroot ATTRIBUTE_UNUSED,
+ const char *iprefix ATTRIBUTE_UNUSED,
+ int stdinc ATTRIBUTE_UNUSED);
+#undef TARGET_EXTRA_PRE_INCLUDES
+#define TARGET_EXTRA_PRE_INCLUDES msp430_register_pre_includes
+
#undef USE_SELECT_SECTION_FOR_FUNCTIONS
#define USE_SELECT_SECTION_FOR_FUNCTIONS 1