function(kicad_gerber) set(options ALL UPPERCASE_EXTENSIONS SHOW_OUTPUTS EXTENDED_GERBER_ATTRIBUTES) set(one_value_args TARGET PCB_FILE DIR ZIP_FILE NAMING_STYLE) set(multi_value_args LAYER_EXTENSION) cmake_parse_arguments(ARGS "${options}" "${one_value_args}" "${multi_value_args}" ${ARGN}) if (NOT ARGS_PCB_FILE) message(SEND_ERROR "Missing required argument: PCB_FILE") return() endif () if (NOT ARGS_NAMING_STYLE) set(ARGS_NAMING_STYLE MODERN) endif () if (ARGS_NAMING_STYLE STREQUAL MODERN) elseif (ARGS_NAMING_STYLE STREQUAL PROTEL) set(protel --protel-extensions) else () message(SEND_ERROR "Unsupported NAMING_STYLE: ${ARGS_NAMING_STYLE}") return() endif () set(pcb_file "${ARGS_PCB_FILE}") get_filename_component(pcb_file "${ARGS_PCB_FILE}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") get_filename_component(basename "${pcb_file}" NAME_WE) if (NOT ARGS_TARGET) set(target "${basename}-gerber") else () set(target "${ARGS_TARGET}") endif () if (NOT ARGS_DIR) message(SEND_ERROR "Missing required argument: DIR") return() endif () foreach (le ${ARGS_LAYER_EXTENSION}) list(APPEND les --layer-extension ${le}) endforeach () if (ARGS_UPPERCASE_EXTENSIONS) set(uppercase_extensions --uppercase-extensions) endif () if (ARGS_EXTENDED_GERBER_ATTRIBUTES) set(extended_gerber_attributes "--extended-gerber-attributes") endif () set(out_dir "${ARGS_DIR}") set(prefix "${out_dir}/${basename}") if(NOT IS_ABSOLUTE "${out_dir}") set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${ARGS_DIR}") endif() execute_process( COMMAND "${KicadUtilsPyDir}/kicad_gerber.py" --pcb "${pcb_file}" --output-directory "${out_dir}" --detect-files-only ${CMAKE_CURRENT_BINARY_DIR}/${target}-gerber-index.txt ${protel} ${uppercase_extensions} ${extended_gerber_attributes} ${les} OUTPUT_VARIABLE out ERROR_VARIABLE out RESULT_VARIABLE res) if (NOT res EQUAL 0) message(SEND_ERROR "kicad_gerber.py failed. Output ${out}") return() endif () file(STRINGS ${CMAKE_CURRENT_BINARY_DIR}/${target}-gerber-index.txt outputs) list(SORT outputs) if (ARGS_SHOW_OUTPUTS) foreach(o ${outputs}) get_filename_component(o ${o} NAME) message("Gerber output file: ${o}") endforeach() endif() add_custom_command( OUTPUT ${outputs} COMMAND cmake -E make_directory "${out_dir}" COMMAND "${KicadUtilsPyDir}/kicad_gerber.py" --pcb "${pcb_file}" --output-directory "${out_dir}" ${protel} ${uppercase_extensions} ${extended_gerber_attributes} ${les} MAIN_DEPENDENCY "${pcb_file}") if (ARGS_ALL) set(all ALL) endif () if (ARGS_ZIP_FILE) if (NOT IS_ABSOLUTE ARGS_ZIP_FILE) set(ARGS_ZIP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${ARGS_ZIP_FILE}) endif () add_custom_command( OUTPUT ${ARGS_ZIP_FILE} MAIN_DEPENDENCY "${pcb_file}" DEPENDS ${outputs} COMMAND cmake -E remove -f ${ARGS_ZIP_FILE} COMMAND zip -q ${ARGS_ZIP_FILE} -j ${outputs}) endif () add_custom_target( ${target} ${all} DEPENDS ${outputs} ${ARGS_ZIP_FILE}) endfunction()