Beispiel #1
0
    def run(self, ext_dep, _inputs=None):
        """Verify the cache file contents, if it exists"""
        cache_ok = False
        filename = self.get_action_file(ext_dep, "cache")
        self.log_debug("Checking settings in {}".format(filename))
        with LineReader.OpenFileCM(filename, "rt") as cache_cm:
            if cache_cm:
                self.log_debug("Reading values")
                # Not providing a real target class is OK as long as we don't
                # try to stringize the LineInfo object.
                reader = LineReader.ReadLineInfoIter(cache_cm.get_file(),
                                                     target="")
                cache_lines = [line_info.get_text() for line_info in reader]
                if cache_lines == self.arg:
                    cache_ok = True
                else:
                    self.log_debug("Expected:")
                    for line in self.arg:
                        self.log_debug("  '{}'".format(line))
                    self.log_debug("Cached:")
                    for line in cache_lines:
                        self.log_debug("  '{}'".format(line))
            else:
                self.log_debug("No valid cache; (re)running actions")

        return cache_ok
Beispiel #2
0
    def generate(self, definitions, out_dir):
        """Generate a settings file"""
        java_file = self.filename.lower()
        package_dir = os.path.dirname(java_file)
        package_name = os.path.relpath(package_dir, start=out_dir)
        package_name = self.PACKAGE_RE.sub(".", package_name)
        class_name = self.CLASS_RE.sub("", os.path.basename(self.filename))
        os.makedirs(Util.get_abs_path(package_dir, out_dir), exist_ok=True)
        with LineReader.OpenFileCM(java_file, "wt") as hdr_cm:
            if not hdr_cm:
                Log.E("Could not open {}: {}".format(self.filename,
                                                     hdr_cm.get_err_msg()))

            hdr_file = hdr_cm.get_file()

            # File header
            hdr_file.write("\n".join([
                "/* GENERATED BY GLOBIFEST -- DO NOT EDIT */", "",
                "package {}".format(package_name), "",
                "public final class {}".format(class_name), "{\n"
            ]))

            # Add values
            template = "    public final static {} {} = {};\n"
            for d in definitions:
                ptype = d.param.get_type()
                pid = d.param.get_identifier()
                value = d.value

                # Write implicit values first
                implicit_id = None
                for implicit_value in d.param.get_implicit_values():
                    hdr_file.write(
                        template.format("int", implicit_value[0],
                                        implicit_value[1]))
                    if value == implicit_value[1]:
                        implicit_id = implicit_value[0]

                # Write the parameter
                if ptype == DefTree.PARAM_TYPE.INT:
                    hdr_file.write(template.format("int", pid, value))
                elif ptype == DefTree.PARAM_TYPE.FLOAT:
                    # Default type is double precision
                    hdr_file.write(template.format("double", pid, value))
                elif ptype == DefTree.PARAM_TYPE.STRING:
                    hdr_file.write(template.format("String", pid, value))
                elif ptype == DefTree.PARAM_TYPE.BOOL:
                    hdr_file.write(
                        template.format("boolean", pid, value.lower()))
                elif ptype == DefTree.PARAM_TYPE.ENUM:
                    if implicit_id:
                        value = implicit_id
                    hdr_file.write(template.format("int", pid, value))
                else:
                    # TODO: Handle more complex literal types
                    Log.E("Unhandled value of type {}".format(
                        str(d.param.ptype)))

            # File footer
            hdr_file.write("}\n")
Beispiel #3
0
    def generate(self, definitions, out_dir):
        """Generate a settings file"""
        include_guard = os.path.relpath(self.filename, start=out_dir)
        include_guard = "_{}".format(
            CGenerator.INCLUDE_GUARD_REPLACE.sub("_", include_guard))
        include_guard = include_guard.upper()
        with LineReader.OpenFileCM(self.filename, "wt") as hdr_cm:
            if not hdr_cm:
                Log.E("Could not open {}: {}".format(self.filename,
                                                     hdr_cm.get_err_msg()))

            hdr_file = hdr_cm.get_file()

            # File header
            hdr_file.write("\n".join([
                "/* GENERATED BY GLOBIFEST -- DO NOT EDIT */", "",
                "#ifndef {}".format(include_guard),
                "#define {}".format(include_guard), "\n"
            ]))

            # Add values; preprocessor directives are used for maximum type flexibility
            for d in definitions:
                ptype = d.param.get_type()
                pid = d.param.get_identifier()
                value = d.value

                # Write implicit values first
                implicit_id = None
                for implicit_value in d.param.get_implicit_values():
                    hdr_file.write("#define {} ({})\n".format(
                        implicit_value[0], implicit_value[1]))
                    if value == implicit_value[1]:
                        implicit_id = implicit_value[0]

                # Write the parameter
                if ptype in [DefTree.PARAM_TYPE.INT, DefTree.PARAM_TYPE.FLOAT]:
                    # Parentheses prevent conflicts with surrounding code
                    # Default type of INT is int (i.e., signed  literal)
                    # Default type of FLOAT is double precision
                    hdr_file.write("#define {} ({})\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.STRING:
                    # Strings are not surrounded to allow compile-time concatenation
                    hdr_file.write("#define {} {}\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.BOOL:
                    # Define as 1/0, since C89 did not define TRUE and FALSE values
                    if value == "FALSE":
                        value = 0
                    else:
                        value = 1
                    hdr_file.write("#define {} ({})\n".format(pid, value))
                elif ptype == DefTree.PARAM_TYPE.ENUM:
                    if implicit_id:
                        hdr_file.write("#define {} {}\n".format(
                            pid, implicit_id))
                    else:
                        hdr_file.write("#define {} ({})\n".format(pid, value))
                else:
                    # TODO: Handle more complex literal types:
                    # - Integral types U/L/UL/LL/ULL
                    # - Float suffixes F/L for floats
                    Log.E("Unhandled value of type {}".format(
                        str(d.param.ptype)))

            # File footer
            hdr_file.write("\n#endif /* {} */\n".format(include_guard))