Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 1 | import os |
Caleb Palmer | e2e645e | 2024-09-03 15:34:45 -0500 | [diff] [blame] | 2 | import sys |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 3 | |
| 4 | from parse_chip_data import gen_peltool_json |
| 5 | from setuptools import setup |
| 6 | from setuptools.command.build_py import build_py |
| 7 | |
| 8 | # Typically in files like this we'd use find_packages() to traverse directories |
| 9 | # for any static packages. However, we are trying to add data to a package that |
| 10 | # will actually exist in another repository. Therefore, we have to explicitly |
| 11 | # list out the package name, directory, and data information. |
| 12 | |
| 13 | # We are building data for the following module: |
| 14 | package_name = "pel.hwdiags" |
| 15 | |
| 16 | # Since we are not using find_packages() we have to provide a package directory, |
| 17 | # but in this case nothing exists because there are no static package |
| 18 | # directories. Therefore, we will just use the empty string. |
| 19 | package_dir = "" |
| 20 | |
| 21 | # Split the package data directory into its components. |
| 22 | data_dir_components = [*package_name.split("."), "data"] |
| 23 | |
| 24 | # It is important to note that '/' must be used as the path separator, even on |
| 25 | # Windows. Setuptools will automatically convert the slashes where appropriate. |
| 26 | package_data_glob = "/".join(data_dir_components) |
| 27 | |
Caleb Palmer | e2e645e | 2024-09-03 15:34:45 -0500 | [diff] [blame] | 28 | # These are the possible chip config options to build the PEL parser data json |
| 29 | # from. By default all configs will be built. The '--chip-config' option can be |
| 30 | # used in recipes to specify manually. |
| 31 | chipConfig = ["p10_10", "p10_20", "explorer", "odyssey"] |
| 32 | |
| 33 | # The chip-config option is manually handled here, rather than added as a custom |
| 34 | # option in the my_build_py class to facilitate use with setuptools which will |
| 35 | # use bdist_wheel in the build. There is currently not a way to indicate a |
| 36 | # option only for the custom build_py through bdist_wheel. As such the option |
| 37 | # will be handled generically here with the chipConfig variable only used in the |
| 38 | # custom build class. |
| 39 | if "--chip-config" in sys.argv: |
| 40 | chipConfig = sys.argv.pop(sys.argv.index("--chip-config") + 1).split(",") |
| 41 | sys.argv.remove("--chip-config") |
| 42 | |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 43 | |
| 44 | # This is a custom build class that is used to dynamically build the data files. |
| 45 | class my_build_py(build_py): |
Caleb Palmer | ae60d8e | 2024-08-28 16:09:20 -0500 | [diff] [blame] | 46 | |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 47 | def run(self): |
| 48 | if not self.dry_run: # honor --dry-run flag |
| 49 | # Make sure the build directory for the data exists. |
| 50 | # Yes, os.path.join() is necessary in this case, which is different |
| 51 | # that what is stated above regarding package_data_glob. |
| 52 | data_dir = os.path.join(self.build_lib, *data_dir_components) |
| 53 | self.mkpath(data_dir) |
| 54 | |
| 55 | # Generate the PEL parser data JSON from the Chip Data XML. |
Caleb Palmer | e2e645e | 2024-09-03 15:34:45 -0500 | [diff] [blame] | 56 | for chip in chipConfig: |
Zane Shelley | 2215d23 | 2023-04-07 14:43:40 -0500 | [diff] [blame] | 57 | gen_peltool_json(chip, data_dir) |
| 58 | |
| 59 | # Call the superclass run() to ensure everything else builds. |
| 60 | super().run() |
| 61 | |
| 62 | |
| 63 | setup( |
| 64 | name="openpower-hw-diags-pel-parser-data", |
| 65 | version=os.getenv("PELTOOL_VERSION", "1.0"), |
| 66 | classifiers=["License :: OSI Approved :: Apache Software License"], |
| 67 | cmdclass={"build_py": my_build_py}, # register custom build class |
| 68 | packages=[package_name], |
| 69 | package_dir={package_name: package_dir}, |
| 70 | package_data={package_name: [package_data_glob]}, |
| 71 | ) |