|
| 1 | +# Copyright (c) 2024 - 2025 Munich Quantum Software Stack Project |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License v2.0 with LLVM Exceptions (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://github.com/Munich-Quantum-Software-Stack/QDMI/blob/develop/LICENSE.md |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations under |
| 14 | +# the License. |
| 15 | +# |
| 16 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 17 | + |
| 18 | +# Script-mode generator for the QDMI device template. |
| 19 | +# |
| 20 | +# Expected variables (passed via -D...): - QDMI_TEMPLATE_SOURCE_DIR: path to |
| 21 | +# templates/device - QDMI_TEMPLATE_OUTPUT_DIR: output directory to |
| 22 | +# create/populate - QDMI_TEMPLATE_PREFIX: uppercase prefix, e.g. "ABC" - |
| 23 | +# QDMI_TEMPLATE_FORCE: ON/OFF, overwrite destination if it already exists |
| 24 | + |
| 25 | +cmake_minimum_required(VERSION 3.24) |
| 26 | + |
| 27 | +foreach(var IN ITEMS QDMI_TEMPLATE_SOURCE_DIR QDMI_TEMPLATE_OUTPUT_DIR |
| 28 | + QDMI_TEMPLATE_PREFIX) |
| 29 | + if(NOT DEFINED ${var} OR "${${var}}" STREQUAL "") |
| 30 | + message(FATAL_ERROR "[qdmi-template] Missing required -D${var}=...") |
| 31 | + endif() |
| 32 | +endforeach() |
| 33 | + |
| 34 | +set(QDMI_TEMPLATE_FORCE "${QDMI_TEMPLATE_FORCE}") |
| 35 | +if("${QDMI_TEMPLATE_FORCE}" STREQUAL "") |
| 36 | + set(QDMI_TEMPLATE_FORCE OFF) |
| 37 | +endif() |
| 38 | + |
| 39 | +file(REAL_PATH "${QDMI_TEMPLATE_SOURCE_DIR}" QDMI_TEMPLATE_SOURCE_DIR) |
| 40 | +file(REAL_PATH "${QDMI_TEMPLATE_OUTPUT_DIR}" QDMI_TEMPLATE_OUTPUT_DIR) |
| 41 | + |
| 42 | +string(TOLOWER "${QDMI_TEMPLATE_PREFIX}" QDMI_TEMPLATE_prefix) |
| 43 | + |
| 44 | +if(NOT EXISTS "${QDMI_TEMPLATE_SOURCE_DIR}") |
| 45 | + message( |
| 46 | + FATAL_ERROR |
| 47 | + "[qdmi-template] Source directory does not exist: ${QDMI_TEMPLATE_SOURCE_DIR}" |
| 48 | + ) |
| 49 | +endif() |
| 50 | + |
| 51 | +# Destination handling |
| 52 | +if(EXISTS "${QDMI_TEMPLATE_OUTPUT_DIR}") |
| 53 | + if(QDMI_TEMPLATE_FORCE) |
| 54 | + message( |
| 55 | + STATUS |
| 56 | + "[qdmi-template] Removing existing output dir: ${QDMI_TEMPLATE_OUTPUT_DIR}" |
| 57 | + ) |
| 58 | + file(REMOVE_RECURSE "${QDMI_TEMPLATE_OUTPUT_DIR}") |
| 59 | + else() |
| 60 | + message( |
| 61 | + FATAL_ERROR |
| 62 | + "[qdmi-template] Output directory already exists: ${QDMI_TEMPLATE_OUTPUT_DIR}. " |
| 63 | + "Re-run with -DQDMI_TEMPLATE_FORCE=ON to overwrite.") |
| 64 | + endif() |
| 65 | +endif() |
| 66 | + |
| 67 | +message( |
| 68 | + STATUS |
| 69 | + "[qdmi-template] Generating device template for prefix '${QDMI_TEMPLATE_PREFIX}' in '${QDMI_TEMPLATE_OUTPUT_DIR}'" |
| 70 | +) |
| 71 | + |
| 72 | +# Copy directory skeleton first |
| 73 | +file(MAKE_DIRECTORY "${QDMI_TEMPLATE_OUTPUT_DIR}") |
| 74 | + |
| 75 | +# Collect all files under the template source dir, but only regular files. |
| 76 | +file( |
| 77 | + GLOB_RECURSE _files |
| 78 | + LIST_DIRECTORIES false |
| 79 | + "${QDMI_TEMPLATE_SOURCE_DIR}/*") |
| 80 | + |
| 81 | +# Apply substitutions file-by-file. |
| 82 | +foreach(_src IN LISTS _files) |
| 83 | + file(RELATIVE_PATH _rel "${QDMI_TEMPLATE_SOURCE_DIR}" "${_src}") |
| 84 | + get_filename_component(_rel_dir "${_rel}" DIRECTORY) |
| 85 | + get_filename_component(_name "${_rel}" NAME) |
| 86 | + |
| 87 | + # Rename path segments / filenames similar to the previous configure-time |
| 88 | + # logic. |
| 89 | + set(_dest_rel_dir "${_rel_dir}") |
| 90 | + set(_dest_name "${_name}") |
| 91 | + |
| 92 | + # Directory-level rename: python/my -> python/<prefix> |
| 93 | + string(REPLACE "python/my" "python/${QDMI_TEMPLATE_prefix}" _dest_rel_dir |
| 94 | + "${_dest_rel_dir}") |
| 95 | + |
| 96 | + # Filename renames that include the placeholder |
| 97 | + string(REPLACE "my_" "${QDMI_TEMPLATE_prefix}_" _dest_name "${_dest_name}") |
| 98 | + string(REPLACE "my-" "${QDMI_TEMPLATE_prefix}-" _dest_name "${_dest_name}") |
| 99 | + string(REPLACE "my.qdmi" "${QDMI_TEMPLATE_prefix}.qdmi" _dest_name |
| 100 | + "${_dest_name}") |
| 101 | + |
| 102 | + set(_dest "${QDMI_TEMPLATE_OUTPUT_DIR}/${_dest_rel_dir}/${_dest_name}") |
| 103 | + get_filename_component(_dest_dir "${_dest}" DIRECTORY) |
| 104 | + file(MAKE_DIRECTORY "${_dest_dir}") |
| 105 | + |
| 106 | + # Read content and replace placeholders. |
| 107 | + file(READ "${_src}" _content) |
| 108 | + |
| 109 | + # Be conservative: these are all specific placeholders used by the shipped |
| 110 | + # template. |
| 111 | + string( |
| 112 | + REGEX |
| 113 | + REPLACE "set\\(QDMI_PREFIX \"MY\"\\)" |
| 114 | + "set(QDMI_PREFIX \"${QDMI_TEMPLATE_PREFIX}\")" _content |
| 115 | + "${_content}") |
| 116 | + string(REPLACE "MY_" "${QDMI_TEMPLATE_PREFIX}_" _content "${_content}") |
| 117 | + string(REPLACE "MY QDMI" "${QDMI_TEMPLATE_PREFIX} QDMI" _content |
| 118 | + "${_content}") |
| 119 | + string(REPLACE "my_" "${QDMI_TEMPLATE_prefix}_" _content "${_content}") |
| 120 | + string(REPLACE "my-" "${QDMI_TEMPLATE_prefix}-" _content "${_content}") |
| 121 | + string(REPLACE "python/my" "python/${QDMI_TEMPLATE_prefix}" _content |
| 122 | + "${_content}") |
| 123 | + string(REPLACE "my/qdmi" "${QDMI_TEMPLATE_prefix}/qdmi" _content |
| 124 | + "${_content}") |
| 125 | + string(REPLACE "my-qdmi" "${QDMI_TEMPLATE_prefix}-qdmi" _content |
| 126 | + "${_content}") |
| 127 | + string(REPLACE "my.qdmi" "${QDMI_TEMPLATE_prefix}.qdmi" _content |
| 128 | + "${_content}") |
| 129 | + string(REPLACE "\"my\"" "\"${QDMI_TEMPLATE_PREFIX}\"" _content "${_content}") |
| 130 | + |
| 131 | + file(WRITE "${_dest}" "${_content}") |
| 132 | +endforeach() |
| 133 | + |
| 134 | +# Explicit folder/file renames that can't be expressed purely via filename |
| 135 | +# mapping above. (These are safe no-ops if the file doesn't exist in future |
| 136 | +# template revisions.) |
| 137 | +if(EXISTS "${QDMI_TEMPLATE_OUTPUT_DIR}/python/my") |
| 138 | + file(RENAME "${QDMI_TEMPLATE_OUTPUT_DIR}/python/my" |
| 139 | + "${QDMI_TEMPLATE_OUTPUT_DIR}/python/${QDMI_TEMPLATE_prefix}") |
| 140 | +endif() |
| 141 | + |
| 142 | +foreach( |
| 143 | + _p IN |
| 144 | + ITEMS |
| 145 | + "src/my_device.cpp;src/${QDMI_TEMPLATE_prefix}_device.cpp" |
| 146 | + "test/test_my_device.cpp;test/test_${QDMI_TEMPLATE_prefix}_device.cpp" |
| 147 | + "cmake/my-qdmi-device-config.cmake.in;cmake/${QDMI_TEMPLATE_prefix}-qdmi-device-config.cmake.in" |
| 148 | + "docs/_static/my_logo.svg;docs/_static/${QDMI_TEMPLATE_prefix}_logo.svg" |
| 149 | + "docs/_static/my_logo_dark.svg;docs/_static/${QDMI_TEMPLATE_prefix}_logo_dark.svg" |
| 150 | +) |
| 151 | + string(REPLACE ";" ";" _p "${_p}") |
| 152 | + list(GET _p 0 _from) |
| 153 | + list(GET _p 1 _to) |
| 154 | + if(EXISTS "${QDMI_TEMPLATE_OUTPUT_DIR}/${_from}") |
| 155 | + file(RENAME "${QDMI_TEMPLATE_OUTPUT_DIR}/${_from}" |
| 156 | + "${QDMI_TEMPLATE_OUTPUT_DIR}/${_to}") |
| 157 | + endif() |
| 158 | +endforeach() |
| 159 | + |
| 160 | +message(STATUS "[qdmi-template] Generation done") |
0 commit comments