rusefi/java_tools/configuration_definition/src/main/java/com/rusefi/ConfigDefinition.java

194 lines
8.1 KiB
Java
Raw Normal View History

package com.rusefi;
2023-06-17 11:07:21 -07:00
import com.rusefi.newparse.DefinitionsState;
2019-09-10 19:55:58 -07:00
import com.rusefi.output.*;
2023-02-11 11:29:17 -08:00
import com.rusefi.pinout.PinoutLogic;
import com.rusefi.trigger.TriggerWheelTSLogic;
2023-07-02 21:56:19 -07:00
import com.rusefi.util.LazyFile;
import com.rusefi.util.SystemOut;
2019-05-18 14:10:28 -07:00
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
2020-07-02 13:28:07 -07:00
import java.util.*;
/**
2020-06-09 17:08:16 -07:00
* Andrey Belomutskiy, (c) 2013-2020
* 1/12/15
*
2021-04-26 14:51:29 -07:00
* @see ConfigurationConsumer
*/
public class ConfigDefinition {
public static final String SIGNATURE_HASH = "SIGNATURE_HASH";
2019-05-26 17:54:44 -07:00
2022-04-15 18:57:08 -07:00
private static final String KEY_DEFINITION = "-definition";
private static final String KEY_TS_TEMPLATE = "-ts_template";
2022-04-15 18:57:08 -07:00
private static final String KEY_C_DESTINATION = "-c_destination";
2019-05-27 14:44:10 -07:00
private static final String KEY_C_DEFINES = "-c_defines";
public static final String KEY_WITH_C_DEFINES = "-with_c_defines";
2022-04-15 18:57:08 -07:00
private static final String KEY_JAVA_DESTINATION = "-java_destination";
private static final String KEY_FIRING = "-firing_order";
public static final String KEY_PREPEND = "-prepend";
2022-01-04 20:34:32 -08:00
private static final String KEY_SIGNATURE = "-signature";
private static final String KEY_SIGNATURE_DESTINATION = "-signature_destination";
private static final String KEY_ZERO_INIT = "-initialize_to_zero";
private static final String KEY_BOARD_NAME = "-board";
public static final String CONFIG_PATH = "java_tools/configuration_definition/src/main/resources/config_definition.options";
2020-08-14 12:38:56 -07:00
public static void main(String[] args) {
try {
List<String> options = Files.readAllLines(Paths.get("../" + CONFIG_PATH));
options.addAll(Arrays.asList(args));
String[] totalArgs = options.toArray(new String[0]);
if (totalArgs.length < 2) {
SystemOut.println("Please specify\r\n"
+ KEY_DEFINITION + " x\r\n"
+ KEY_TS_TEMPLATE + " x\r\n"
+ KEY_C_DESTINATION + " x\r\n"
+ KEY_JAVA_DESTINATION + " x\r\n"
);
return;
}
doJob(totalArgs, new ReaderStateImpl());
} catch (Throwable e) {
SystemOut.println(e);
e.printStackTrace();
System.exit(-1);
2023-06-17 10:51:46 -07:00
} finally {
SystemOut.close();
}
}
public static void doJob(String[] args, ReaderStateImpl state) throws IOException {
2021-07-13 06:01:08 -07:00
SystemOut.println(ConfigDefinition.class + " Invoked with " + Arrays.toString(args));
String tsTemplateFile = null;
2023-06-17 11:07:21 -07:00
DefinitionsState parseState = state.getEnumsReader().parseState;
String signatureDestination = null;
String signaturePrependFile = null;
List<String> enumInputFiles = new ArrayList<>();
PinoutLogic pinoutLogic = null;
2019-05-26 15:37:53 -07:00
for (int i = 0; i < args.length - 1; i += 2) {
String key = args[i];
switch (key) {
case "-tool":
// lame: order of command line arguments is important
ToolUtil.TOOL = args[i + 1];
break;
case KEY_DEFINITION:
2022-04-13 14:55:31 -07:00
// lame: order of command line arguments is important, these arguments should be AFTER '-tool' argument
2022-04-15 18:41:52 -07:00
state.setDefinitionInputFile(args[i + 1]);
break;
case KEY_TS_TEMPLATE:
tsTemplateFile = args[i + 1];
2021-11-26 20:48:23 -08:00
break;
case KEY_C_DESTINATION:
2022-04-15 18:57:08 -07:00
state.addCHeaderDestination(args[i + 1]);
break;
case KEY_ZERO_INIT:
BaseCHeaderConsumer.needZeroInit = Boolean.parseBoolean(args[i + 1]);
break;
case KEY_WITH_C_DEFINES:
2023-01-06 07:30:26 -08:00
state.setWithC_Defines(Boolean.parseBoolean(args[i + 1]));
break;
case KEY_C_DEFINES:
2023-06-17 11:07:21 -07:00
state.destCDefinesFileName = args[i + 1];
break;
case KEY_JAVA_DESTINATION:
2022-04-15 18:57:08 -07:00
state.addJavaDestination(args[i + 1]);
break;
case "-field_lookup_file": {
String cppFile = args[i + 1];
String mdFile = args[i + 2];
i++;
2023-07-02 21:56:19 -07:00
state.addDestination(new GetConfigValueConsumer(cppFile, mdFile, LazyFile.REAL));
}
2021-12-15 16:43:31 -08:00
break;
case "-readfile":
String keyName = args[i + 1];
// yes, we take three parameters here thus pre-increment!
String fileName = args[++i + 1];
2022-04-18 09:22:50 -07:00
try {
2023-01-06 07:42:44 -08:00
state.getVariableRegistry().register(keyName, IoUtil2.readFile(fileName));
2022-04-18 09:22:50 -07:00
} catch (RuntimeException e) {
throw new IllegalStateException("While processing " + fileName, e);
}
2023-01-06 07:35:07 -08:00
state.addInputFile(fileName);
2023-06-17 11:49:03 -07:00
break;
2023-06-17 11:07:21 -07:00
case KEY_FIRING: {
String firingEnumFileName = args[i + 1];
ExtraUtil.handleFiringOrder(firingEnumFileName, state.getVariableRegistry(), parseState);
2023-01-06 07:35:07 -08:00
state.addInputFile(firingEnumFileName);
2023-06-17 11:07:21 -07:00
}
break;
2023-06-17 11:07:21 -07:00
case "-triggerInputFolder": {
String triggersInputFolder = args[i + 1];
new TriggerWheelTSLogic().execute(triggersInputFolder, state.getVariableRegistry());
}
break;
case KEY_PREPEND:
2022-04-15 18:57:08 -07:00
state.addPrepend(args[i + 1].trim());
break;
case KEY_SIGNATURE:
signaturePrependFile = args[i + 1];
2023-01-06 07:42:44 -08:00
state.getPrependFiles().add(args[i + 1]);
// don't add this file to the 'inputFiles'
break;
case KEY_SIGNATURE_DESTINATION:
signatureDestination = args[i + 1];
break;
case EnumToString.KEY_ENUM_INPUT_FILE: {
String enumInputFile = args[i + 1];
enumInputFiles.add(enumInputFile);
2024-03-09 18:39:55 -08:00
File file = new File(RootHolder.ROOT + enumInputFile);
try (Reader r = new FileReader(file)) {
state.read(r);
} catch (Throwable e) {
throw new IllegalStateException("Reading " + file.getAbsolutePath(), e);
}
}
break;
case "-ts_output_name":
2023-01-06 07:42:44 -08:00
state.setTsFileOutputName(args[i + 1]);
break;
case KEY_BOARD_NAME:
String boardName = args[i + 1];
pinoutLogic = PinoutLogic.create(boardName);
2023-02-11 12:20:10 -08:00
for (String inputFile : pinoutLogic.getInputFiles())
state.addInputFile(inputFile);
break;
2019-05-26 15:37:53 -07:00
}
}
if (tsTemplateFile != null) {
2021-04-29 19:26:20 -07:00
// used to update .ini files
state.addInputFile(tsTemplateFile);
}
SystemOut.println(state.getEnumsReader().getEnums().size() + " total enumsReader");
// Add the variable for the config signature
2023-01-06 07:35:07 -08:00
FirmwareVersion uniqueId = new FirmwareVersion(IoUtil2.getCrc32(state.getInputFiles()));
SignatureConsumer.storeUniqueBuildId(state, parseState, tsTemplateFile, uniqueId);
if (pinoutLogic != null) {
pinoutLogic.registerBoardSpecificPinNames(state.getVariableRegistry(), parseState, state.getEnumsReader());
}
if (tsTemplateFile != null) {
state.addDestination(new TSProjectConsumer(tsTemplateFile, state));
VariableRegistry tmpRegistry = new VariableRegistry();
// store the CRC32 as a built-in variable
2022-10-27 19:12:39 -07:00
tmpRegistry.register(SIGNATURE_HASH, uniqueId.encode());
tmpRegistry.readPrependValues(signaturePrependFile);
2023-01-06 07:42:44 -08:00
state.addDestination(new SignatureConsumer(signatureDestination, tmpRegistry));
2019-05-26 15:37:53 -07:00
}
2022-04-16 10:57:16 -07:00
state.doJob();
2019-05-26 15:37:53 -07:00
}
}