1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2023, BUGSENG Srl
4
5find_program(ECLAIR_ENV eclair_env REQUIRED)
6message(STATUS "Found eclair_env: ${ECLAIR_ENV}")
7
8find_program(ECLAIR_REPORT eclair_report REQUIRED)
9message(STATUS "Found eclair_report: ${ECLAIR_REPORT}")
10
11# Get eclair specific option file variables, also needed if invoked with sysbuild
12zephyr_get(ECLAIR_OPTIONS_FILE)
13
14if(ECLAIR_OPTIONS_FILE)
15  if(IS_ABSOLUTE ${ECLAIR_OPTIONS_FILE})
16    set(ECLAIR_OPTIONS ${ECLAIR_OPTIONS_FILE})
17  else()
18    set(ECLAIR_OPTIONS ${APPLICATION_CONFIG_DIR}/${ECLAIR_OPTIONS_FILE})
19  endif()
20  include(${ECLAIR_OPTIONS})
21else()
22  include(${CMAKE_CURRENT_LIST_DIR}/sca_options.cmake)
23endif()
24
25# ECLAIR Settings
26set(ECLAIR_PROJECT_NAME "Zephyr-${BOARD}${BOARD_QUALIFIERS}")
27set(ECLAIR_OUTPUT_DIR "${CMAKE_BINARY_DIR}/sca/eclair")
28set(ECLAIR_ECL_DIR "${ZEPHYR_BASE}/cmake/sca/eclair/ECL")
29set(ECLAIR_ANALYSIS_ECL_DIR "${ZEPHYR_BASE}/cmake/sca/eclair/ECL")
30set(ECLAIR_DIAGNOSTICS_OUTPUT "${ECLAIR_OUTPUT_DIR}/DIAGNOSTIC.txt")
31set(ECLAIR_ANALYSIS_DATA_DIR "${ECLAIR_OUTPUT_DIR}/analysis_data")
32set(ECLAIR_PROJECT_ECD "${ECLAIR_OUTPUT_DIR}/PROJECT.ecd")
33set(CC_ALIASES "${CMAKE_C_COMPILER}")
34set(CXX_ALIASES "${CMAKE_CXX_COMPILER}")
35set(AS_ALIASES "${CMAKE_AS}")
36set(LD_ALIASES "${CMAKE_LINKER}")
37set(AR_ALIASES "${CMAKE_ASM_COMPILER_AR} ${CMAKE_C_COMPILER_AR} ${CMAKE_CXX_COMPILER_AR}")
38
39set(ECLAIR_ENV_ADDITIONAL_OPTIONS "")
40set(ECLAIR_REPORT_ADDITIONAL_OPTIONS "")
41
42# Default value
43set(ECLAIR_RULESET first_analysis)
44
45# ECLAIR env
46if(ECLAIR_RULESET_FIRST_ANALYSIS)
47  set(ECLAIR_RULESET first_analysis)
48elseif(ECLAIR_RULESET_STU)
49  set(ECLAIR_RULESET STU)
50elseif(ECLAIR_RULESET_STU_HEAVY)
51  set(ECLAIR_RULESET STU_heavy)
52elseif(ECLAIR_RULESET_WP)
53  set(ECLAIR_RULESET WP)
54elseif(ECLAIR_RULESET_STD_LIB)
55  set(ECLAIR_RULESET std_lib)
56elseif(ECLAIR_RULESET_USER)
57  set(ECLAIR_RULESET ${ECLAIR_USER_RULESET_NAME})
58  if(IS_ABSOLUTE ${ECLAIR_USER_RULESET_PATH})
59      set(ECLAIR_ANALYSIS_ECL_DIR ${ECLAIR_USER_RULESET_PATH})
60  else()
61    set(ECLAIR_ANALYSIS_ECL_DIR ${APPLICATION_CONFIG_DIR}/${ECLAIR_USER_RULESET_PATH})
62  endif()
63endif()
64
65# ECLAIR report
66if (ECLAIR_METRICS_TAB)
67  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-metrics_tab=${ECLAIR_OUTPUT_DIR}/metrics")
68endif()
69if (ECLAIR_REPORTS_TAB)
70  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-reports_tab=${ECLAIR_OUTPUT_DIR}/reports")
71endif()
72if (ECLAIR_REPORTS_SARIF)
73  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-reports_sarif=${ECLAIR_OUTPUT_DIR}/reports.sarif")
74endif()
75if (ECLAIR_SUMMARY_TXT)
76  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-summary_txt=${ECLAIR_OUTPUT_DIR}/summary_txt")
77endif()
78if (ECLAIR_SUMMARY_DOC)
79  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-summary_doc=${ECLAIR_OUTPUT_DIR}/summary_doc")
80endif()
81if (ECLAIR_SUMMARY_ODT)
82  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-summary_odt=${ECLAIR_OUTPUT_DIR}/summary_odt")
83endif()
84if (ECLAIR_FULL_TXT_ALL_AREAS)
85  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-setq=report_areas,areas")
86endif()
87if (ECLAIR_FULL_TXT_FIRST_AREA)
88  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-setq=report_areas,first_area")
89endif()
90if (ECLAIR_FULL_TXT)
91  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-full_txt=${ECLAIR_OUTPUT_DIR}/report_full_txt")
92endif()
93if (ECLAIR_FULL_DOC_ALL_AREAS)
94  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-setq=report_areas,areas")
95endif()
96if (ECLAIR_FULL_DOC_FIRST_AREA)
97  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-setq=report_areas,first_area")
98endif()
99if (ECLAIR_FULL_DOC)
100  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-full_doc=${ECLAIR_OUTPUT_DIR}/report_full_doc")
101endif()
102if (ECLAIR_FULL_ODT)
103  list(APPEND ECLAIR_REPORT_ADDITIONAL_OPTIONS "-full_odt=${ECLAIR_OUTPUT_DIR}/report_full_odt")
104endif()
105
106message(STATUS "ECLAIR outputs have been written to: ${ECLAIR_OUTPUT_DIR}")
107message(STATUS "ECLAIR ECB files have been written to: ${ECLAIR_ANALYSIS_DATA_DIR}")
108
109add_custom_target(eclair_setup_analysis_dir ALL
110  COMMAND ${CMAKE_COMMAND} -E remove_directory ${ECLAIR_ANALYSIS_DATA_DIR}
111  COMMAND ${CMAKE_COMMAND} -E make_directory ${ECLAIR_ANALYSIS_DATA_DIR}
112  VERBATIM
113  USES_TERMINAL
114)
115
116# configure the camke script which will be used to replace the compiler call with the eclair_env
117# call which calls the compiler and to generate analysis files.
118configure_file(${CMAKE_CURRENT_LIST_DIR}/eclair.template ${ECLAIR_OUTPUT_DIR}/eclair.cmake @ONLY)
119
120set(launch_environment ${CMAKE_COMMAND} -P ${ECLAIR_OUTPUT_DIR}/eclair.cmake --)
121set(CMAKE_C_COMPILER_LAUNCHER ${launch_environment} CACHE INTERNAL "")
122
123# This target is used to generate the ECLAIR database when all the compilation is done and the
124# elf file was generated with this we cane make sure that the analysis is completed.
125add_custom_target(eclair_report ALL
126  COMMAND ${CMAKE_COMMAND} -E env
127    ECLAIR_DATA_DIR=${ECLAIR_ANALYSIS_DATA_DIR}
128    ECLAIR_OUTPUT_DIR=${ECLAIR_OUTPUT_DIR}
129    ECLAIR_PROJECT_ECD=${ECLAIR_PROJECT_ECD}
130    ${ECLAIR_REPORT} -quiet -eval_file=${ECLAIR_ECL_DIR}/db_generation.ecl
131  DEPENDS ${CMAKE_BINARY_DIR}/zephyr/zephyr.elf
132  VERBATIM
133  USES_TERMINAL
134  COMMAND_EXPAND_LISTS
135)
136
137# This command is used to generate the final reports from the database and print the overall results
138add_custom_target(eclair_summary_print ALL
139  COMMAND ${ECLAIR_REPORT}
140    -db=${ECLAIR_PROJECT_ECD} ${ECLAIR_REPORT_ADDITIONAL_OPTIONS}
141    -overall_txt=${ECLAIR_OUTPUT_DIR}/summary_overall.txt
142  COMMAND ${CMAKE_COMMAND} -E cat ${ECLAIR_OUTPUT_DIR}/summary_overall.txt
143)
144add_dependencies(eclair_summary_print eclair_report)
145