├── .github └── workflows │ ├── build-and-test.yaml │ ├── check-formatting.yaml │ └── reuse-lint.yaml ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── LICENSES ├── Apache-2.0.txt ├── CC-BY-4.0.txt ├── CC0-1.0.txt └── LGPL-2.1-or-later.txt ├── Makefile ├── README.md ├── UPDATING.md ├── install.sh ├── rebar.config ├── rebar.lock ├── release └── packbeam.in ├── src ├── atomvm_packbeam.app.src ├── packbeam.erl └── packbeam_api.erl └── test ├── a.erl ├── b.erl ├── c.erl ├── d.erl ├── e.erl ├── f.erl ├── priv ├── test.txt └── test.txt.license ├── prop_packbeam.erl ├── test_packbeam.erl └── x.erl /.github/workflows/build-and-test.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2022 Fred Dushin 3 | # 4 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 5 | # 6 | 7 | name: Build and Test 8 | 9 | on: [push, pull_request] 10 | 11 | jobs: 12 | build-and-test: 13 | runs-on: "ubuntu-24.04" 14 | strategy: 15 | matrix: 16 | otp: ["25", "26", "27", "28"] 17 | permissions: 18 | contents: read 19 | 20 | steps: 21 | # Setup 22 | - name: "Checkout repo" 23 | uses: actions/checkout@v2 24 | with: 25 | submodules: 'recursive' 26 | 27 | - uses: erlef/setup-beam@v1 28 | with: 29 | otp-version: ${{ matrix.otp }} 30 | 31 | # Builder info 32 | - name: "System info" 33 | run: | 34 | echo "**uname:**" 35 | uname -a 36 | echo "**OTP version:**" 37 | cat $(dirname $(which erlc))/../releases/RELEASES || true 38 | 39 | - name: "Install deps" 40 | run: | 41 | sudo apt install -y make git 42 | 43 | - name: "Build rebar3" 44 | run: | 45 | cd /tmp 46 | git clone https://github.com/erlang/rebar3.git 47 | cd rebar3 48 | ./bootstrap 49 | 50 | # Build 51 | - name: "Make" 52 | run: PATH="/tmp/rebar3:${PATH}" make 53 | -------------------------------------------------------------------------------- /.github/workflows/check-formatting.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2022 Davide Bettio 3 | # Copyright 2025 Winford (Uncle Grumpy) 4 | # 5 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | # 7 | 8 | name: "Check Erlang Formatting" 9 | 10 | on: 11 | push: 12 | paths: 13 | - 'src/**' 14 | - 'test/**' 15 | - '**/*.erl' 16 | pull_request: 17 | paths: 18 | - 'src/**' 19 | - 'test/**' 20 | - '**/*.erl' 21 | 22 | jobs: 23 | format-check: 24 | runs-on: ubuntu-24.04 25 | container: erlang:28 26 | steps: 27 | - name: "Install deps" 28 | run: | 29 | apt install -y git 30 | 31 | - name: "Install erlfmt" 32 | run: | 33 | cd ${HOME} 34 | git clone --depth 1 -b v1.6.2 https://github.com/WhatsApp/erlfmt.git 35 | cd erlfmt 36 | rebar3 as release escriptize 37 | 38 | - uses: actions/checkout@v4 39 | 40 | - name: "Check formatting with erlfmt" 41 | run: | 42 | find . -name *.erl | xargs ${HOME}/erlfmt/_build/release/bin/erlfmt -c 43 | -------------------------------------------------------------------------------- /.github/workflows/reuse-lint.yaml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Free Software Foundation Europe e.V. 2 | # 3 | # SPDX-License-Identifier: CC0-1.0 4 | 5 | name: REUSE Compliance Check 6 | 7 | on: [push, pull_request] 8 | 9 | concurrency: 10 | group: ${{ github.workflow }}-${{ github.ref != 'refs/heads/main' && github.ref || github.run_id }} 11 | cancel-in-progress: true 12 | 13 | jobs: 14 | test: 15 | runs-on: ubuntu-24.04 16 | steps: 17 | - uses: actions/checkout@v4 18 | - name: REUSE Compliance Check 19 | uses: fsfe/reuse-action@bb774aa972c2a89ff34781233d275075cbddf542 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Copyright 2020 Fred Dushin 3 | # 4 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 5 | 6 | _build 7 | _checkouts 8 | rebar3.crashdump 9 | erl_crash.dump 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 6 | # Changelog 7 | All notable changes to this project will be documented in this file. 8 | 9 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 10 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 11 | 12 | ## [0.7.4] - (2025.05.25) 13 | 14 | - Add support for OTP-28 15 | - Added missing licences files, and CI workflow to ensure reuse compliance 16 | 17 | >This release includes changes from 0.7.3 that were not included in the release due to a bad release tag. 18 | 19 | ## [0.7.3] (2024.06.06) 20 | 21 | - Fix broken `create` task in the cli tool. 22 | 23 | ## [0.7.2] (2023.11.24) 24 | 25 | - Make use of profiles to minimize downstream dependencies 26 | - Fix install.sh script on FreeBSD platform 27 | 28 | ## [0.7.1] (2023.10.22) 29 | 30 | - Enhanced `packbeam_api` to make it more maintainable. 31 | - Changed documentation to use [`rebar3_ex_doc`](https://hexdocs.pm/rebar3_ex_doc/readme.html) 32 | 33 | ## [0.7.0] (2022.10.17) 34 | 35 | - Added `version` sub-command to print version to the console 36 | - Added `-r`, `--remove` option and removed the `-i`, `--include` option, which was ineffective due to a bug. See the [Updating](UPDATING.md) notes on the impact of these changes. 37 | 38 | ## [0.6.2] (2023.10.14) 39 | 40 | - Added `relx` stanzas to create a standalone release of the `packbeam` utility 41 | - Added install scripts to simplify installation for users 42 | 43 | ## [0.6.1] (2023.07.16) 44 | 45 | - Added `extract` sub-command 46 | 47 | ## [0.6.0] (2022.12.18) 48 | 49 | ### Added 50 | - Added ability to include `<<"Line">>` chunks in BEAM files in generated AVM files 51 | - Added CI build 52 | 53 | ### Changed 54 | - Changed the `packbeam_api:create` function to take a single map for optional 55 | parameters, instead of coding paramters into function arguments. Previous 56 | versions of the `packbeam_api:create` function that take optional parameters 57 | have been deprecated. 58 | 59 | ## [0.5.0] (2022.08.28) 60 | 61 | ### Added 62 | - Added ability to specify a module name for ordinary (non-BEAM) files (API-only). 63 | - Added support for tracking dependencies using application spec files as binaries 64 | (API-only) 65 | - Added PropEr test 66 | - Added new `format` option to the `list` subcommand, supporting `csv`, `bare`, 67 | and `default` options. 68 | 69 | ### Fixed 70 | - Fixed a bug in parsing non-BEAM files in included AVM files, which would cause 71 | non-BEAM file contents to be loaded incorrectly. 72 | 73 | ### Changed 74 | - Changed the command line syntax to support long and short option names using 75 | GNU-style conventions; deprecated single-hyphen short options. 76 | - Moved `packbeam` API functionality into `packbeam_api` module. 77 | Previous `packbeam` API functions now call corresponding `packbeam_api` 78 | functions and are deprecated. 79 | 80 | ## [0.4.1] (2022.06.19) 81 | 82 | ### Added 83 | - Added unit tests 84 | 85 | ### Fixed 86 | - Fixed a bug that failed to track atoms that occur in BEAM LitT tables 87 | 88 | ### Changed 89 | - Weakened the test for finding a start BEAM file such that it only requires that the `?BEAM_START_FLAG` be set, for compatibility with [ExAtomVM](https://github.com/atomvm/ExAtomVM). 90 | 91 | ## [0.4.0] (2022.05.21) 92 | 93 | ### Added 94 | - Added `erlfmt` plugin and formatted code. 95 | 96 | ### Fixed 97 | - Fixed a bug that prevented packbeam files that include priv files from being properly loaded. 98 | 99 | ## [0.3.0] (2022.01.15) 100 | 101 | ### Fixed 102 | - Fixed a bug in pulling in dependent BEAM files based on the atoms table 103 | 104 | ## [0.2.0] (2021.04.03) 105 | 106 | ### Added 107 | - Added support for deployment to hex 108 | 109 | ### Fixed 110 | - Uncompressed literals table 111 | 112 | ## [0.1.0] (2020.05.17) 113 | - Initial Release 114 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | # Contributor Covenant Code of Conduct 8 | 9 | ## Our Pledge 10 | 11 | We as members, contributors, and leaders pledge to make participation in our 12 | community a harassment-free experience for everyone, regardless of age, body 13 | size, visible or invisible disability, ethnicity, sex characteristics, gender 14 | identity and expression, level of experience, education, socio-economic status, 15 | nationality, personal appearance, race, religion, or sexual identity 16 | and orientation. 17 | 18 | We pledge to act and interact in ways that contribute to an open, welcoming, 19 | diverse, inclusive, and healthy community. 20 | 21 | ## Our Standards 22 | 23 | Examples of behavior that contributes to a positive environment for our 24 | community include: 25 | 26 | * Demonstrating empathy and kindness toward other people 27 | * Being respectful of differing opinions, viewpoints, and experiences 28 | * Giving and gracefully accepting constructive feedback 29 | * Accepting responsibility and apologizing to those affected by our mistakes, 30 | and learning from the experience 31 | * Focusing on what is best not just for us as individuals, but for the 32 | overall community 33 | 34 | Examples of unacceptable behavior include: 35 | 36 | * The use of sexualized language or imagery, and sexual attention or 37 | advances of any kind 38 | * Trolling, insulting or derogatory comments, and personal or political attacks 39 | * Public or private harassment 40 | * Publishing others' private information, such as a physical or email 41 | address, without their explicit permission 42 | * Other conduct which could reasonably be considered inappropriate in a 43 | professional setting 44 | 45 | ## Enforcement Responsibilities 46 | 47 | Community leaders are responsible for clarifying and enforcing our standards of 48 | acceptable behavior and will take appropriate and fair corrective action in 49 | response to any behavior that they deem inappropriate, threatening, offensive, 50 | or harmful. 51 | 52 | Community leaders have the right and responsibility to remove, edit, or reject 53 | comments, commits, code, wiki edits, issues, and other contributions that are 54 | not aligned to this Code of Conduct, and will communicate reasons for moderation 55 | decisions when appropriate. 56 | 57 | ## Scope 58 | 59 | This Code of Conduct applies within all community spaces, and also applies when 60 | an individual is officially representing the community in public spaces. 61 | Examples of representing our community include using an official e-mail address, 62 | posting via an official social media account, or acting as an appointed 63 | representative at an online or offline event. 64 | 65 | ## Enforcement 66 | 67 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 68 | reported to the community leaders responsible for enforcement at 69 | davide AT uninstall.it. 70 | All complaints will be reviewed and investigated promptly and fairly. 71 | 72 | All community leaders are obligated to respect the privacy and security of the 73 | reporter of any incident. 74 | 75 | ## Enforcement Guidelines 76 | 77 | Community leaders will follow these Community Impact Guidelines in determining 78 | the consequences for any action they deem in violation of this Code of Conduct: 79 | 80 | ### 1. Correction 81 | 82 | **Community Impact**: Use of inappropriate language or other behavior deemed 83 | unprofessional or unwelcome in the community. 84 | 85 | **Consequence**: A private, written warning from community leaders, providing 86 | clarity around the nature of the violation and an explanation of why the 87 | behavior was inappropriate. A public apology may be requested. 88 | 89 | ### 2. Warning 90 | 91 | **Community Impact**: A violation through a single incident or series 92 | of actions. 93 | 94 | **Consequence**: A warning with consequences for continued behavior. No 95 | interaction with the people involved, including unsolicited interaction with 96 | those enforcing the Code of Conduct, for a specified period of time. This 97 | includes avoiding interactions in community spaces as well as external channels 98 | like social media. Violating these terms may lead to a temporary or 99 | permanent ban. 100 | 101 | ### 3. Temporary Ban 102 | 103 | **Community Impact**: A serious violation of community standards, including 104 | sustained inappropriate behavior. 105 | 106 | **Consequence**: A temporary ban from any sort of interaction or public 107 | communication with the community for a specified period of time. No public or 108 | private interaction with the people involved, including unsolicited interaction 109 | with those enforcing the Code of Conduct, is allowed during this period. 110 | Violating these terms may lead to a permanent ban. 111 | 112 | ### 4. Permanent Ban 113 | 114 | **Community Impact**: Demonstrating a pattern of violation of community 115 | standards, including sustained inappropriate behavior, harassment of an 116 | individual, or aggression toward or disparagement of classes of individuals. 117 | 118 | **Consequence**: A permanent ban from any sort of public interaction within 119 | the community. 120 | 121 | ## Attribution 122 | 123 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 124 | version 2.0, available at 125 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 126 | 127 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 128 | enforcement ladder](https://github.com/mozilla/diversity). 129 | 130 | [homepage]: https://www.contributor-covenant.org 131 | 132 | For answers to common questions about this code of conduct, see the FAQ at 133 | https://www.contributor-covenant.org/faq. Translations are available at 134 | https://www.contributor-covenant.org/translations. 135 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | # Contributing 8 | 9 | Before contributing, please read carefully our [Code of Conduct](CODE_OF_CONDUCT.md) and 10 | the following contribution guidelines. 11 | 12 | Please, also make sure to understand the [Apache 2.0 license](LICENSE) and the 13 | [Developer Certificate of Origin](https://developercertificate.org/). 14 | 15 | Last but not least, **do not use GitHub issues for vulnerability reports**, read instead the 16 | [security policy](SECURITY.md) for instructions. 17 | 18 | ## Git Recommended Practises 19 | 20 | * Commit messages should have a 21 | * [summary and a description](https://github.com/erlang/otp/wiki/writing-good-commit-messages) 22 | * Avoid trailing white spaces 23 | * Always `git pull --rebase` 24 | * [Clean up your branch history](https://git-scm.com/book/id/v2/Git-Tools-Rewriting-History) with 25 | `git rebase -i` 26 | * All your intermediate commits should build 27 | 28 | ## Coding Style 29 | 30 | ### C Code 31 | 32 | #### Identation 33 | 34 | * [K&R identation and braces style](https://en.wikipedia.org/wiki/Indentation_style#K&R_style) 35 | * [Mandatory braces](https://en.wikipedia.org/wiki/Indentation_style#Variant:_mandatory_braces) 36 | * 4 spaces identation 37 | 38 | Good: 39 | ``` 40 | void f(int reverse) 41 | { 42 | if (reverse) { 43 | puts("!dlroW olleH"); 44 | } else { 45 | puts("Hello world"); 46 | } 47 | } 48 | ``` 49 | 50 | Bad: 51 | ``` 52 | void f(int reverse) { 53 | if (reverse) 54 | puts ("!dlroW olleH"); 55 | else 56 | puts ("Hello world"); 57 | } 58 | ``` 59 | 60 | #### Names 61 | 62 | * Struct names are PascalCase (e.g. Context) 63 | * Scalar types are lower case (e.g. term) 64 | * All other names (e.g. functions and variables) are snake_case (e.g. term_is_integer) 65 | * Always prefix function names (e.g. term_is_nil, term_is_integer, context_new, context_destroy) 66 | 67 | #### Other Coding Conventions 68 | * Pointer * should be with the variable name rather than with the type (e.g. `char *name`, not 69 | `char* name`) 70 | * Avoid long lines, use intermediate variables with meaningful names. 71 | 72 | ### Elixir Code 73 | 74 | Just use Elixir formatter enforced style. 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2020, Fred Dushin . 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | -------------------------------------------------------------------------------- /LICENSES/Apache-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /LICENSES/CC-BY-4.0.txt: -------------------------------------------------------------------------------- 1 | Attribution 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution 4.0 International Public License 58 | 59 | By exercising the Licensed Rights (defined below), You accept and agree 60 | to be bound by the terms and conditions of this Creative Commons 61 | Attribution 4.0 International Public License ("Public License"). To the 62 | extent this Public License may be interpreted as a contract, You are 63 | granted the Licensed Rights in consideration of Your acceptance of 64 | these terms and conditions, and the Licensor grants You such rights in 65 | consideration of benefits the Licensor receives from making the 66 | Licensed Material available under these terms and conditions. 67 | 68 | 69 | Section 1 -- Definitions. 70 | 71 | a. Adapted Material means material subject to Copyright and Similar 72 | Rights that is derived from or based upon the Licensed Material 73 | and in which the Licensed Material is translated, altered, 74 | arranged, transformed, or otherwise modified in a manner requiring 75 | permission under the Copyright and Similar Rights held by the 76 | Licensor. For purposes of this Public License, where the Licensed 77 | Material is a musical work, performance, or sound recording, 78 | Adapted Material is always produced where the Licensed Material is 79 | synched in timed relation with a moving image. 80 | 81 | b. Adapter's License means the license You apply to Your Copyright 82 | and Similar Rights in Your contributions to Adapted Material in 83 | accordance with the terms and conditions of this Public License. 84 | 85 | c. Copyright and Similar Rights means copyright and/or similar rights 86 | closely related to copyright including, without limitation, 87 | performance, broadcast, sound recording, and Sui Generis Database 88 | Rights, without regard to how the rights are labeled or 89 | categorized. For purposes of this Public License, the rights 90 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 91 | Rights. 92 | 93 | d. Effective Technological Measures means those measures that, in the 94 | absence of proper authority, may not be circumvented under laws 95 | fulfilling obligations under Article 11 of the WIPO Copyright 96 | Treaty adopted on December 20, 1996, and/or similar international 97 | agreements. 98 | 99 | e. Exceptions and Limitations means fair use, fair dealing, and/or 100 | any other exception or limitation to Copyright and Similar Rights 101 | that applies to Your use of the Licensed Material. 102 | 103 | f. Licensed Material means the artistic or literary work, database, 104 | or other material to which the Licensor applied this Public 105 | License. 106 | 107 | g. Licensed Rights means the rights granted to You subject to the 108 | terms and conditions of this Public License, which are limited to 109 | all Copyright and Similar Rights that apply to Your use of the 110 | Licensed Material and that the Licensor has authority to license. 111 | 112 | h. Licensor means the individual(s) or entity(ies) granting rights 113 | under this Public License. 114 | 115 | i. Share means to provide material to the public by any means or 116 | process that requires permission under the Licensed Rights, such 117 | as reproduction, public display, public performance, distribution, 118 | dissemination, communication, or importation, and to make material 119 | available to the public including in ways that members of the 120 | public may access the material from a place and at a time 121 | individually chosen by them. 122 | 123 | j. Sui Generis Database Rights means rights other than copyright 124 | resulting from Directive 96/9/EC of the European Parliament and of 125 | the Council of 11 March 1996 on the legal protection of databases, 126 | as amended and/or succeeded, as well as other essentially 127 | equivalent rights anywhere in the world. 128 | 129 | k. You means the individual or entity exercising the Licensed Rights 130 | under this Public License. Your has a corresponding meaning. 131 | 132 | 133 | Section 2 -- Scope. 134 | 135 | a. License grant. 136 | 137 | 1. Subject to the terms and conditions of this Public License, 138 | the Licensor hereby grants You a worldwide, royalty-free, 139 | non-sublicensable, non-exclusive, irrevocable license to 140 | exercise the Licensed Rights in the Licensed Material to: 141 | 142 | a. reproduce and Share the Licensed Material, in whole or 143 | in part; and 144 | 145 | b. produce, reproduce, and Share Adapted Material. 146 | 147 | 2. Exceptions and Limitations. For the avoidance of doubt, where 148 | Exceptions and Limitations apply to Your use, this Public 149 | License does not apply, and You do not need to comply with 150 | its terms and conditions. 151 | 152 | 3. Term. The term of this Public License is specified in Section 153 | 6(a). 154 | 155 | 4. Media and formats; technical modifications allowed. The 156 | Licensor authorizes You to exercise the Licensed Rights in 157 | all media and formats whether now known or hereafter created, 158 | and to make technical modifications necessary to do so. The 159 | Licensor waives and/or agrees not to assert any right or 160 | authority to forbid You from making technical modifications 161 | necessary to exercise the Licensed Rights, including 162 | technical modifications necessary to circumvent Effective 163 | Technological Measures. For purposes of this Public License, 164 | simply making modifications authorized by this Section 2(a) 165 | (4) never produces Adapted Material. 166 | 167 | 5. Downstream recipients. 168 | 169 | a. Offer from the Licensor -- Licensed Material. Every 170 | recipient of the Licensed Material automatically 171 | receives an offer from the Licensor to exercise the 172 | Licensed Rights under the terms and conditions of this 173 | Public License. 174 | 175 | b. No downstream restrictions. You may not offer or impose 176 | any additional or different terms or conditions on, or 177 | apply any Effective Technological Measures to, the 178 | Licensed Material if doing so restricts exercise of the 179 | Licensed Rights by any recipient of the Licensed 180 | Material. 181 | 182 | 6. No endorsement. Nothing in this Public License constitutes or 183 | may be construed as permission to assert or imply that You 184 | are, or that Your use of the Licensed Material is, connected 185 | with, or sponsored, endorsed, or granted official status by, 186 | the Licensor or others designated to receive attribution as 187 | provided in Section 3(a)(1)(A)(i). 188 | 189 | b. Other rights. 190 | 191 | 1. Moral rights, such as the right of integrity, are not 192 | licensed under this Public License, nor are publicity, 193 | privacy, and/or other similar personality rights; however, to 194 | the extent possible, the Licensor waives and/or agrees not to 195 | assert any such rights held by the Licensor to the limited 196 | extent necessary to allow You to exercise the Licensed 197 | Rights, but not otherwise. 198 | 199 | 2. Patent and trademark rights are not licensed under this 200 | Public License. 201 | 202 | 3. To the extent possible, the Licensor waives any right to 203 | collect royalties from You for the exercise of the Licensed 204 | Rights, whether directly or through a collecting society 205 | under any voluntary or waivable statutory or compulsory 206 | licensing scheme. In all other cases the Licensor expressly 207 | reserves any right to collect such royalties. 208 | 209 | 210 | Section 3 -- License Conditions. 211 | 212 | Your exercise of the Licensed Rights is expressly made subject to the 213 | following conditions. 214 | 215 | a. Attribution. 216 | 217 | 1. If You Share the Licensed Material (including in modified 218 | form), You must: 219 | 220 | a. retain the following if it is supplied by the Licensor 221 | with the Licensed Material: 222 | 223 | i. identification of the creator(s) of the Licensed 224 | Material and any others designated to receive 225 | attribution, in any reasonable manner requested by 226 | the Licensor (including by pseudonym if 227 | designated); 228 | 229 | ii. a copyright notice; 230 | 231 | iii. a notice that refers to this Public License; 232 | 233 | iv. a notice that refers to the disclaimer of 234 | warranties; 235 | 236 | v. a URI or hyperlink to the Licensed Material to the 237 | extent reasonably practicable; 238 | 239 | b. indicate if You modified the Licensed Material and 240 | retain an indication of any previous modifications; and 241 | 242 | c. indicate the Licensed Material is licensed under this 243 | Public License, and include the text of, or the URI or 244 | hyperlink to, this Public License. 245 | 246 | 2. You may satisfy the conditions in Section 3(a)(1) in any 247 | reasonable manner based on the medium, means, and context in 248 | which You Share the Licensed Material. For example, it may be 249 | reasonable to satisfy the conditions by providing a URI or 250 | hyperlink to a resource that includes the required 251 | information. 252 | 253 | 3. If requested by the Licensor, You must remove any of the 254 | information required by Section 3(a)(1)(A) to the extent 255 | reasonably practicable. 256 | 257 | 4. If You Share Adapted Material You produce, the Adapter's 258 | License You apply must not prevent recipients of the Adapted 259 | Material from complying with this Public License. 260 | 261 | 262 | Section 4 -- Sui Generis Database Rights. 263 | 264 | Where the Licensed Rights include Sui Generis Database Rights that 265 | apply to Your use of the Licensed Material: 266 | 267 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 268 | to extract, reuse, reproduce, and Share all or a substantial 269 | portion of the contents of the database; 270 | 271 | b. if You include all or a substantial portion of the database 272 | contents in a database in which You have Sui Generis Database 273 | Rights, then the database in which You have Sui Generis Database 274 | Rights (but not its individual contents) is Adapted Material; and 275 | 276 | c. You must comply with the conditions in Section 3(a) if You Share 277 | all or a substantial portion of the contents of the database. 278 | 279 | For the avoidance of doubt, this Section 4 supplements and does not 280 | replace Your obligations under this Public License where the Licensed 281 | Rights include other Copyright and Similar Rights. 282 | 283 | 284 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 285 | 286 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 287 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 288 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 289 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 290 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 291 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 292 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 293 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 294 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 295 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 296 | 297 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 298 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 299 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 300 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 301 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 302 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 303 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 304 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 305 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 306 | 307 | c. The disclaimer of warranties and limitation of liability provided 308 | above shall be interpreted in a manner that, to the extent 309 | possible, most closely approximates an absolute disclaimer and 310 | waiver of all liability. 311 | 312 | 313 | Section 6 -- Term and Termination. 314 | 315 | a. This Public License applies for the term of the Copyright and 316 | Similar Rights licensed here. However, if You fail to comply with 317 | this Public License, then Your rights under this Public License 318 | terminate automatically. 319 | 320 | b. Where Your right to use the Licensed Material has terminated under 321 | Section 6(a), it reinstates: 322 | 323 | 1. automatically as of the date the violation is cured, provided 324 | it is cured within 30 days of Your discovery of the 325 | violation; or 326 | 327 | 2. upon express reinstatement by the Licensor. 328 | 329 | For the avoidance of doubt, this Section 6(b) does not affect any 330 | right the Licensor may have to seek remedies for Your violations 331 | of this Public License. 332 | 333 | c. For the avoidance of doubt, the Licensor may also offer the 334 | Licensed Material under separate terms or conditions or stop 335 | distributing the Licensed Material at any time; however, doing so 336 | will not terminate this Public License. 337 | 338 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 339 | License. 340 | 341 | 342 | Section 7 -- Other Terms and Conditions. 343 | 344 | a. The Licensor shall not be bound by any additional or different 345 | terms or conditions communicated by You unless expressly agreed. 346 | 347 | b. Any arrangements, understandings, or agreements regarding the 348 | Licensed Material not stated herein are separate from and 349 | independent of the terms and conditions of this Public License. 350 | 351 | 352 | Section 8 -- Interpretation. 353 | 354 | a. For the avoidance of doubt, this Public License does not, and 355 | shall not be interpreted to, reduce, limit, restrict, or impose 356 | conditions on any use of the Licensed Material that could lawfully 357 | be made without permission under this Public License. 358 | 359 | b. To the extent possible, if any provision of this Public License is 360 | deemed unenforceable, it shall be automatically reformed to the 361 | minimum extent necessary to make it enforceable. If the provision 362 | cannot be reformed, it shall be severed from this Public License 363 | without affecting the enforceability of the remaining terms and 364 | conditions. 365 | 366 | c. No term or condition of this Public License will be waived and no 367 | failure to comply consented to unless expressly agreed to by the 368 | Licensor. 369 | 370 | d. Nothing in this Public License constitutes or may be interpreted 371 | as a limitation upon, or waiver of, any privileges and immunities 372 | that apply to the Licensor or You, including from the legal 373 | processes of any jurisdiction or authority. 374 | 375 | 376 | ======================================================================= 377 | 378 | Creative Commons is not a party to its public 379 | licenses. Notwithstanding, Creative Commons may elect to apply one of 380 | its public licenses to material it publishes and in those instances 381 | will be considered the “Licensor.” The text of the Creative Commons 382 | public licenses is dedicated to the public domain under the CC0 Public 383 | Domain Dedication. Except for the limited purpose of indicating that 384 | material is shared under a Creative Commons public license or as 385 | otherwise permitted by the Creative Commons policies published at 386 | creativecommons.org/policies, Creative Commons does not authorize the 387 | use of the trademark "Creative Commons" or any other trademark or logo 388 | of Creative Commons without its prior written consent including, 389 | without limitation, in connection with any unauthorized modifications 390 | to any of its public licenses or any other arrangements, 391 | understandings, or agreements concerning use of licensed material. For 392 | the avoidance of doubt, this paragraph does not form part of the 393 | public licenses. 394 | 395 | Creative Commons may be contacted at creativecommons.org. 396 | 397 | -------------------------------------------------------------------------------- /LICENSES/CC0-1.0.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /LICENSES/LGPL-2.1-or-later.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 489 | 490 | Also add information on how to contact you by electronic and paper mail. 491 | 492 | You should also get your employer (if you work as a programmer) or your 493 | school, if any, to sign a "copyright disclaimer" for the library, if 494 | necessary. Here is a sample; alter the names: 495 | 496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 498 | 499 | , 1 April 1990 500 | Ty Coon, President of Vice 501 | 502 | That's all there is to it! 503 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Copyright (c) 2020 dushin.net 3 | ## All rights reserved. 4 | ## 5 | ## SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | all: compile escript etest doc rel 8 | 9 | compile: 10 | rebar3 compile 11 | 12 | escript: 13 | rebar3 escriptize 14 | 15 | doc: 16 | rebar3 as doc ex_doc 17 | 18 | etest: 19 | rebar3 as test eunit --cover 20 | rebar3 as test proper --cover 21 | rebar3 as test cover --verbose 22 | 23 | rel: 24 | rebar3 as prod release 25 | rebar3 as prod tar 26 | rm -rf x 27 | mkdir x 28 | ./install.sh x 0.7.4 29 | x/bin/packbeam version 30 | 31 | clean: 32 | rm -rf _build 33 | 34 | publish: doc 35 | rebar3 as publish hex publish --doc-dir docs 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 6 | # `atomvm_packbeam` 7 | 8 | An Erlang Escript and library used to generate an [AtomVM](http://github.com/atomvm/AtomVM) AVM file from a set of files (beam files, previously built AVM files, or even arbitrary data files). 9 | 10 | This tool roughly approximates the functionality of the AtomVM `PackBEAM` utility, except: 11 | 12 | * Support for multiple data types, include beam files, text files, etc 13 | * "Pruned" extraction of beams from AVM files, so that only the beams that are needed are packed 14 | * Support for embedded OTP applications in your PackBEAM files. 15 | 16 | The `packbeam` tool may be used on its own as a stand-alone command-line utility. More typically, it is used internally as part of the [`atomvm_rebar3_plugin`](https://github.com/atomvm/atomvm_rebar3_plugin) [`rebar3`](https://rebar3.org) plugin. 17 | 18 | ## Prerequisites 19 | 20 | Building `packbeam` requires a version of Erlang/OTP compatible with [AtomVM](https://github.com/atomvm/AtomVM), as well as a local installation of [`rebar3`](https://rebar3.org). Optionally, any recent version of `make` may be used to simplify builds. Consult the [AtomVM Documentation](https://www.atomvm.net/doc/master/) for information about supported OTP versions. 21 | 22 | ## Build 23 | 24 | To build a release, run the following commands: 25 | 26 | shell$ rebar3 release 27 | shell$ rebar3 as prod tar 28 | 29 | These commands will create an Erlang tar archive containing a versioned release of the `atomvm_packbeam` tool, e.g., 30 | 31 | ... 32 | ===> Tarball successfully created: _build/prod/rel/atomvm_packbeam/atomvm_packbeam-0.6.2.tar.gz 33 | 34 | in your local working directory. 35 | 36 | > IMPORTANT! The files in this tar archive do not contain the `atomvm_packbeam` prefix, so extracting these files without care will create a `bin` and `lib` directory in the location into which files from the archive is extracted. See the example below before proceeding! 37 | 38 | You can use the `install.sh` script to install the `atomvm_packbeam` utility into a location on your local machine. You will need to specify the prefix location into which you want to install the utility, together with it's current version. 39 | 40 | shell$ ./install.sh /opt/atomvm_packbeam 0.6.2 41 | atomvm_packbeam version 0.6.2 installed in /opt/atomvm_packbeam. 42 | 43 | > Note. Some prefix locations may require `root` permissions to write files to. 44 | 45 | Set your `PATH` environment variable to include the `bin` directory of the installation prefix (if not already set), and you should then be able to run the `packbeam` command included therein. 46 | 47 | For example: 48 | 49 | shell$ export PATH=/opt/atomvm_packbeam/bin:$PATH 50 | shell$ packbeam help 51 | Syntax: 52 | packbeam 53 | ... 54 | 55 | ## `packbeam` command 56 | 57 | The `packbeam` command is used to create an AVM file from a list of beam and other file types, to list the contents of an AVM file, or to delete elements from an AVM file. 58 | 59 | The general syntax of the `packbeam` command takes the form: 60 | 61 | packbeam 62 | 63 | On-line help is available via the `help` sub-command: 64 | 65 | shell$ packbeam help 66 | 67 | packbeam version 0.7.4 68 | 69 | Syntax: 70 | packbeam 71 | 72 | The following sub-commands are supported: 73 | 74 | create []+ 75 | where: 76 | is the output AVM file, 77 | []+ is a list of one or more input files, 78 | and are among the following: 79 | [--prune|-p] Prune dependencies 80 | [--start|-s ] Start module 81 | [--remove_lines|-r] Remove line number information from AVM files 82 | 83 | list 84 | where: 85 | is an AVM file, 86 | and are among the following: 87 | [--format|-f csv|bare|default] Format output 88 | 89 | extract []* 90 | where: 91 | is an AVM file, 92 | []+ is a list of one or more elements to extract 93 | (if empty, then extract all elements) 94 | and are among the following: 95 | [--out|-o ] Output directory into which to write elements 96 | (if unspecified, use the current working directory) 97 | 98 | delete []+ 99 | where: 100 | is an AVM file, 101 | []+ is a list of one or more elements to delete, 102 | and are among the following: 103 | [--out|-o ] Output AVM file 104 | 105 | version 106 | Print version and exit 107 | 108 | help 109 | Print this help 110 | 111 | The `packbeam` command will return an exit status of 0 on successful completion of a command. An unspecified non-zero value is returned in the event of an error. 112 | 113 | The `packbeam` sub-commands are described in more detail below. 114 | 115 | ### `create` sub-command 116 | 117 | To create an AVM file from a list of beam files, use the `create` sub-command to create an AVM file. The first argument is take to be the output AVM file, following by the files you would like to add, e.g., 118 | 119 | shell$ packbeam create mylib.avm mylib/ebin/mylib.beam mylib/ebin/foo.beam mylib/ebin/bar.beam 120 | 121 | This command will create an AtomVM AVM file suitable for use with AtomVM. 122 | 123 | The input files specified in the create sub-command may be among the following types: 124 | 125 | * compiled BEAM files (typically ending in `.beam`) 126 | * Previously created AVM files 127 | * "Normal" files, e.g., text files, binary files, etc. 128 | 129 | Note that beam files specified are stripped of their path information, inside of the generated AVM file. Any files that have the same name will be added in the order they are listed on the command line. However, AtomVM will only resolve the first such file when loading modules at run-time. 130 | 131 | #### Start Entrypoint 132 | 133 | If you are building an application that provides a start entrypoint (as opposed to a library, suitable for inclusion in another AVM file), then at least one beam module in an AVM file must contain a `start/0` entry-point, i.e., a function called `start` with arity 0. AtomVM will use this entry-point as the first function to execute, when starting. 134 | 135 | > Note. It is conventional, but not required, that the first beam file in an AVM file contains the `start/0` entry-point. AtomVM will use the first BEAM file that contains an exported `start/0` function as the entry-point for the application. 136 | 137 | If your application has multiple modules with exported `start/0` functions, you may use the `--start ` (alternatively, `-s `) option to specify the module you would like placed first in your AVM file. The `` parameter should be the module name (without the `.beam` suffix, e.g., `main`). 138 | 139 | A previously created AVM file file may be supplied as input (including the same file specified as output, for example). The contents of any input AVM files will be included in the output AVM file. For example, if you are building a library of BEAM files (for example, none of which contain a `start/0` entry-point), you may want to archive these into an AVM file, which can be used for downstream applications. 140 | 141 | In addition, you may specify a "normal" (i.e., non-beam or non-AVM) file. Normal files are labeled with the path specified on the command line. 142 | 143 | shell$ packbeam create mylib.avm mylib.avm mylib/priv/sample.txt 144 | 145 | > Note. It is conventional in AtomVM for normal files to have the path `/priv/`. 146 | 147 | #### Pruning 148 | 149 | If you specify the `--prune` (alternatively, `-p`) flag, then `packbeam` will only include beam files that are transitively dependent on the entry-point beam. Transitive dependencies are determined by imports, as well as use of an atom in a module (e.g, as the result of a dynamic function call, based on a module name). 150 | 151 | If there is no beam file with a `start/0` entry-point defined in the list of input modules and the `--prune` flag is used, the command will fail. You should _not_ use the `--prune` flag if you are trying to build libraries suitable for inclusion on other AtomVM applications. 152 | 153 | #### Line number information 154 | 155 | By default, the `packbeam` tool will generate line number information for embedded BEAM files. Line number information is included in Erlang stacktraces, giving developers more clues into bugs in their programs. However, line number information does increase the size of AVM files, and in some cases can have an impact on memory in running applications. 156 | 157 | For production applications that have no need for line number information, we recommend using the `-r` (or `--remove_lines`) flags, which will strip line number information from embedded BEAM files. 158 | 159 | ### `list` sub-command 160 | 161 | The `list` sub-command will print the contents of an AVM file to the standard output stream. 162 | 163 | To list the elements of an AVM file, specify the location of the AVM file to input as the first argument: 164 | 165 | shell$ packbeam list mylib.avm 166 | mylib.beam * [284] 167 | foo.beam [276] 168 | bar.beam [252] 169 | mylib/priv/sample.txt [29] 170 | 171 | The elements in the AVM file are printed to the standard output stream and are listed on each line. If a beam file contain an exported `start/0` function, it will be marked with an asterisk (`*`). The size in bytes of each module is also printed in square brackets (`[]`). 172 | 173 | You may use the `--format` (alternatively, `-f`) option to specify an output format. The supported formats are: 174 | 175 | * `csv` Output elements in comma-separated value format. Fields include the module name, whether the element is a BEAM file, whether the element provides a `start/0` entrypoint, and the size (in bytes) of the element. 176 | * `bare` Output just the module name, with no annotations. 177 | * `default` Output the module name, size (in brackets), and whether the file provides a `start/0` entrypoint, indicated by an asterisk (`*`). The `default` output is used if the `--format` option is not specified. 178 | 179 | ### `extract` sub-command 180 | 181 | The `extract` sub-command can be used to extract elements from an AVM file. 182 | 183 | To extract one or more elements from an AVM file, specify the location of the AVM file from which to extract elements, followed by the list of elements (as displayed via the `list` sub-command) to extract. If no elements are listed, then all elements from the AVM file will be extracted. 184 | 185 | Non-BEAM ("normal") files that contain paths in their names will be extracted into a directory tree that reflects the path used in the element name. For example, if the element name is `mylib/priv/sample.txt`, then the `sample.txt` file will be extracted into the `mylib/priv` directory (relative to the output directory, detailed below). 186 | 187 | You may optionally specify an output directory using the `--out` option, which will contain the extracted contents of the input AVM file. This directory must exist beforehand, or a runtime error will occur. If no output directory is specified, elements will be extracted into the current working directory. 188 | 189 | 190 | For example: 191 | 192 | shell$ mkdir mydir 193 | shell$ packbeam extract -out mydir mylib.avm foo.beam mylib/priv/sample.txt 194 | Writing to mydir ... 195 | x foo.beam 196 | x mylib/priv/sample.txt 197 | 198 | 199 | ### `delete` sub-command 200 | 201 | The `delete` sub-command can be used to remove elements from an AVM file. 202 | 203 | To delete one or more elements from an AVM file, specify the location of the AVM file from which to remove elements, followed by the list of elements (as displayed via the `list` sub-command) to remove. You may optionally specify an output AVM file using the `--out` option, which will contain the contents of the input AVM file, minus the specified elements. If no output AVM is specified, the input AVM file will be overwritten. 204 | 205 | For example: 206 | 207 | shell$ packbeam delete -out mylib2.avm mylib.avm foo.beam bar.beam 208 | shell$ packbeam list mylib2.avm 209 | mylib.beam * [284] 210 | mylib/priv/sample.txt [29] 211 | 212 | ## `packbeam_api` API 213 | 214 | In addition to being an `escript` command-line utility, this project provides an Erlang API and library for manipulating AVM files. Simply include `atomvm_packbeam` as a dependency in your `rebar.config`, and you will have access to this API. 215 | 216 | > For more detailed information about this API, see the [`packbeam_api` Reference](packbeam_api.html). 217 | 218 | ### Creating PackBEAM files 219 | 220 | To create a PackBEAM file, use the `packbeam_api:create/2` function. Specify the output path of the AVM you would like to create, followed by a list of paths to the files that will go into the AVM file. Typically, these paths are a list of BEAM files, though you can also include plain data files, in addition to previously created AVM files. Previously-created AVM files will be copied into the output AVM file. 221 | 222 | > Note. Specify the file system paths to all files. BEAM file path information will be stripped from the AVM element path data. Any plain data files (non-BEAM files) will retain their path information. See the [AtomVM Documentation](https://www.atomvm.net/doc/master/) about how to create plain data files in AVM files that users can retrieved via the `atomvm:read_priv/2` function. 223 | 224 | %% erlang 225 | ok = packbeam_api:create( 226 | "/path/to/output.avm", [ 227 | "/path/to/foo.beam", 228 | "/path/to/bar.beam", 229 | "/path/to/myapp/priv/sample.txt", 230 | "/path/to/some_lib.avm" 231 | ] 232 | ). 233 | 234 | Alternatively, you may specify a set of options with the `packbeam_api:create/3` function, which takes a map as the third parameter. 235 | 236 | | Key | Type | Default | Description | 237 | |-----|------|---------|-------------| 238 | | `prune` | `boolean()` | `false` | Specify whether to prune the output AVM file. Pruned AVM files can take considerably less space and hence may lead to faster development times. | 239 | | `start` | `module()` | n/a | Specify the start module, if it can't be determined automatically from the application. | 240 | | `application` | `module()` | n/a | Specify the application module. The `.app` file will be encoded and included as an element in the AVM file with the path `/priv/application.bin` | 241 | | `include_lines` | `boolean()` | `true` | Specify whether to include line number information in generated AVM files. | 242 | 243 | ### Listing the contents of PackBEAM files 244 | 245 | You can list the contents of PackBEAM files using the `packbeam_api:list/1` function. Specify the file system path to the PackBEAM file you would like to list: 246 | 247 | %% erlang 248 | AVMElements = packbeam_api:list("/path/to/input.avm"). 249 | 250 | The returned `AVMElements` is list of an opaque data structures and should not be interpreted by user applications. However, several functions are exposed to retrieve information about elements in this list. 251 | 252 | To get the element name, use the `packbeam_api:get_element_name/1` function, passing in an AVM element. The return type is a `string()` and represents the path in the AVM file for the AVM element. 253 | 254 | %% erlang 255 | AVMElementName = packbeam_api:get_element_name(AVMElement). 256 | 257 | To get the element data (as a binary) use the `packbeam_api:get_element_data/1` function, passing in an AVM element. The return type is a `binary()` containing the actual data in the AVM element. 258 | 259 | %% erlang 260 | AVMElementData = packbeam_api:get_element_data(AVMElement). 261 | 262 | To get the element module (as an atom) use the `packbeam_api:get_element_module/1` function, passing in an AVM element. The return type is a `module()` and the module name of the AVM element. 263 | 264 | Note that if the AVM element is not a BEAM file, this function returns `undefined`. 265 | 266 | %% erlang 267 | AVMElementModule = packbeam_api:get_element_module(AVMElement). 268 | 269 | To determine if the element is a BEAM file, use the `packbeam_api:is_beam/1` function, passing in an AVM element. The return value is a `boolean()`. 270 | 271 | %% erlang 272 | IsBEAM = packbeam_api:is_beam(AVMElement). 273 | 274 | To determine if the element is an entrypoint BEAM (i.e., it exports a `start/0` function), use the `packbeam_api:is_entrypoint/1` function, passing in an AVM element. The return value is a `boolean()`. 275 | 276 | %% erlang 277 | IsEntrypoint = packbeam_api:is_entrypoint(AVMElement). 278 | 279 | ### Deleting entries from PackBEAM files 280 | 281 | You can delete entries from an AVM file using the `packbeam_api:delete/3` function. Specify the file system path to the PackBEAM file you would like to delete from, the output path you would like to write the new AVM file to, and a list of AVM elements you would like to delete: 282 | 283 | %% erlang 284 | ok = packbeam_api:delete( 285 | "/path/to/input.avm", 286 | "/path/to/ouput.avm", 287 | ["foo.beam", "myapp/priv/sample.txt"] 288 | ). 289 | 290 | > Note. You may specify the same values for the input and output paths. In this case, the input AVM file will be _over-written_ by the new AVM file. 291 | 292 | ### Extracting entries from PackBEAM files 293 | 294 | You can extract elements from an AVM file using the `packbeam_api:extract/3` function. Specify the file system path to the PackBEAM file you would like to extract from, a list of AVM elements you would like to extract, and the output directory into which would like to extract the files: 295 | 296 | %% erlang 297 | ok = packbeam_api:extract( 298 | "/path/to/input.avm", 299 | ["foo.beam", "myapp/priv/sample.txt"], 300 | "/tmp" 301 | ). 302 | -------------------------------------------------------------------------------- /UPDATING.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | # AtomVM Update Instructions 8 | 9 | ## 0.6.* -> 0.7.* 10 | 11 | - The default behavior of not generating line number information in BEAM files has changed. By default, line number information will be generated in BEAM files. You can remove line number information using from BEAM files by using the `-r` (or `--remove_lines`) flags to the `create` subcommand. Note that in versions 0.6 of this tool, the `--include_lines` flag was ignored due to a bug in the code. 12 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # This file is part of AtomVM. 4 | # 5 | # Copyright 2023 Fred Dushin 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 20 | # 21 | 22 | # 23 | # Install script for the packbeam utility. 24 | # 25 | # This script will install the packbeam utility in a given location 26 | # on the user's machine. You must provide the prefix location for the 27 | # installation, in addition to the currently operative version, as 28 | # defined in the relx section of this project's rebar.config. 29 | # 30 | # Syntax: install.sh 31 | # where is the prefix location for the install 32 | # is the current release packbeam version 33 | # 34 | # This script will create a self-contained packbeam installation, including 35 | # ERTS as built for the target platform. Effort is made to not conflict 36 | # with any other ERTS or Erlang installations already on the machine. 37 | # 38 | # After installation, users may run /bin/packbeam on the 39 | # command line. (Some installation tools will provide this automatically 40 | # in the user's PATH) 41 | # 42 | # Set the environemnt variable PACKBEAM_DEBUG to a non-empty string 43 | # to get diagnostic information about the installation. 44 | # 45 | 46 | set -e 47 | 48 | readonly root_dir="$(cd $(dirname $0) && pwd)" 49 | 50 | readonly nargs=$# 51 | if [ ${nargs} -lt 2 ]; then 52 | echo 53 | echo "Syntax: $0 " 54 | echo " where is the prefix location for the install" 55 | echo " is the current release packbeam version" 56 | echo 57 | exit 1 58 | fi 59 | readonly prefix="${1}" 60 | readonly version="${2}" 61 | 62 | if [ ! -e "${prefix}" ]; then 63 | echo "ERROR! Prefix dir ${prefix} must exist!" 64 | exit 1 65 | fi 66 | 67 | echo_run() { 68 | cmd="$@" 69 | if [ -n "${PACKBEAM_DEBUG}" ]; then 70 | echo "# $(date) [$(hostname)]> ${cmd}" 71 | fi 72 | ${cmd} 73 | } 74 | 75 | readonly src_tar="${root_dir}/_build/prod/rel/atomvm_packbeam/atomvm_packbeam-${version}.tar.gz" 76 | if [ ! -e "${src_tar}" ]; then 77 | echo "ERROR! It looks like atomvm_packbeam version ${version} has not been built!" 78 | exit 1 79 | fi 80 | 81 | readonly dest_dir="${prefix}/atomvm_packbeam" 82 | if [ -e "${dest_dir}/bin/packbeam" ] && [ "$(${dest_dir}/bin/packbeam version)" == "${version}" ]; then 83 | echo "ERROR! It looks like ${version} is already installed!" 84 | exit 1 85 | fi 86 | 87 | ## unzip the archive (so that BSD tar can deal with it) 88 | readonly tmp_dir="$(mktemp -d /tmp/atomvm_packbeam.XXXXXX)" 89 | echo_run cp "${src_tar}" "${tmp_dir}/." 90 | echo_run gunzip "${tmp_dir}/atomvm_packbeam-${version}.tar.gz" 91 | 92 | echo_run mkdir -p "${dest_dir}" 93 | echo_run tar -C "${dest_dir}" -x -f "${tmp_dir}/atomvm_packbeam-${version}.tar" 94 | 95 | echo_run mkdir -p "${prefix}/bin" 96 | echo_run mv "${dest_dir}/bin/packbeam.sh" "${prefix}/bin/packbeam" 97 | echo_run chmod 755 "${prefix}/bin/packbeam" 98 | 99 | echo_run rm -rf "${tmp_dir}" 100 | echo "atomvm_packbeam version ${version} installed in ${dest_dir}." 101 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2020 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | {erl_opts, [debug_info]}. 8 | {deps, []}. 9 | 10 | {escript_incl_apps, [atomvm_packbeam]}. 11 | {escript_main_app, atomvm_packbeam}. 12 | {escript_name, packbeam}. 13 | {escript_emu_args, "%%! -escript main packbeam"}. 14 | 15 | {ex_doc, [ 16 | {source_url, <<"https://github.com/atomvm/atomvm_packbeam">>}, 17 | {extras, [ 18 | <<"README.md">>, 19 | <<"CHANGELOG.md">>, 20 | <<"UPDATING.md">>, 21 | <<"LICENSE">>, 22 | <<"CONTRIBUTING.md">>, 23 | <<"CODE_OF_CONDUCT.md">> 24 | ]}, 25 | {main, <<"README.md">>}, 26 | {output, "docs"}, 27 | {api_reference, true}, 28 | {skip_undefined_reference_warnings_on, ["README.md"]} 29 | ]}. 30 | 31 | %% Profiles 32 | {profiles, [ 33 | {test, [ 34 | {erl_opts, [debug_info]}, 35 | {cover_enabled, true}, 36 | {deps, [ 37 | {proper, "1.4.0"} 38 | ]}, 39 | {plugins, [rebar3_proper]} 40 | ]}, 41 | {doc, [ 42 | {plugins, [rebar3_ex_doc]} 43 | ]}, 44 | {publish, [ 45 | {plugins, [rebar3_hex, rebar3_ex_doc]}, 46 | {hex, [{doc, #{provider => ex_doc}}]} 47 | ]} 48 | ]}. 49 | 50 | {relx, [ 51 | {release, {atomvm_packbeam, "0.7.4"}, [ 52 | kernel, 53 | stdlib, 54 | atomvm_packbeam 55 | ]}, 56 | {mode, prod}, 57 | {generate_start_script, false}, 58 | {overlay, [ 59 | {template, "release/packbeam.in", "bin/packbeam.sh"} 60 | ]} 61 | ]}. 62 | 63 | %% make sure escriptize is run before we release 64 | {provider_hooks, [ 65 | {pre, [ 66 | {release, escriptize} 67 | ]} 68 | ]}. 69 | 70 | %% copy the generated escript into the release 71 | {post_hooks, [ 72 | {release, "cp ${REBAR_BUILD_DIR}/bin/packbeam ${REBAR_BUILD_DIR}/rel/atomvm_packbeam/bin/packbeam"} 73 | ]}. 74 | -------------------------------------------------------------------------------- /rebar.lock: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2020 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | []. 8 | -------------------------------------------------------------------------------- /release/packbeam.in: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | # 3 | # This file is part of AtomVM. 4 | # 5 | # Copyright 2023 Fred Dushin 6 | # 7 | # Licensed under the Apache License, Version 2.0 (the "License"); 8 | # you may not use this file except in compliance with the License. 9 | # You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, software 14 | # distributed under the License is distributed on an "AS IS" BASIS, 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | # See the License for the specific language governing permissions and 17 | # limitations under the License. 18 | # 19 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 20 | # 21 | 22 | if [ -L "${0}" ]; then 23 | base="$(dirname $0)/$(readlink $0)" 24 | root_dir="$(cd $(dirname ${base})/.. && pwd)" 25 | else 26 | root_dir="$(cd $(dirname $0)/.. && pwd)" 27 | fi 28 | 29 | pkg_root="${root_dir}/atomvm_packbeam" 30 | 31 | PATH="${pkg_root}/erts-{{ release_erts_version }}/bin:${PATH}" 32 | export PATH 33 | 34 | exec ${pkg_root}/bin/packbeam "$@" 35 | -------------------------------------------------------------------------------- /src/atomvm_packbeam.app.src: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2020 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% Licensed under the Apache License, Version 2.0 (the "License"); 6 | %% you may not use this file except in compliance with the License. 7 | %% You may obtain a copy of the License at 8 | %% 9 | %% http://www.apache.org/licenses/LICENSE-2.0 10 | %% 11 | %% Unless required by applicable law or agreed to in writing, software 12 | %% distributed under the License is distributed on an "AS IS" BASIS, 13 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | %% See the License for the specific language governing permissions and 15 | %% limitations under the License. 16 | %% 17 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 18 | 19 | { 20 | application, 21 | atomvm_packbeam, 22 | [ 23 | {description, 24 | "An escript and library to manipulate (create, list, delete) AtomVM PackBeam files"}, 25 | {vsn, "0.7.4"}, 26 | {registered, []}, 27 | {applications, [kernel, stdlib]}, 28 | {env, []}, 29 | {modules, [packbeam_api]}, 30 | {licenses, ["Apache-2.0"]}, 31 | {pkg_name, "atomvm_packbeam"}, 32 | {links, [{"github", "https://github.com/atomvm/atomvm_packbeam"}]} 33 | ] 34 | }. 35 | -------------------------------------------------------------------------------- /src/packbeam.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2020 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% Licensed under the Apache License, Version 2.0 (the "License"); 6 | %% you may not use this file except in compliance with the License. 7 | %% You may obtain a copy of the License at 8 | %% 9 | %% http://www.apache.org/licenses/LICENSE-2.0 10 | %% 11 | %% Unless required by applicable law or agreed to in writing, software 12 | %% distributed under the License is distributed on an "AS IS" BASIS, 13 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | %% See the License for the specific language governing permissions and 15 | %% limitations under the License. 16 | %% 17 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 18 | 19 | %%----------------------------------------------------------------------------- 20 | %% @doc An escript and OTP library used to generate an 21 | %% AtomVM AVM file from a set of 22 | %% files (beam files, previously built AVM files, or even arbitrary data files). 23 | %% @end 24 | %%----------------------------------------------------------------------------- 25 | -module(packbeam). 26 | 27 | %% API exports (for backwards compatibility) 28 | -export([create/2, create/4, list/1, delete/3]). 29 | %% escript 30 | -export([main/1]). 31 | 32 | %% 33 | %% Public API 34 | %% 35 | 36 | %%----------------------------------------------------------------------------- 37 | %% @doc Deprecated. Use the packbeam_api module, instead. 38 | %% @end 39 | %%----------------------------------------------------------------------------- 40 | create(OutputPath, InputPaths) -> 41 | io:format("WARNING. packbeam:create/2 is deprecated. Use packbeam_api::create/2 instead.~n"), 42 | packbeam_api:create(OutputPath, InputPaths). 43 | 44 | %%----------------------------------------------------------------------------- 45 | %% @doc Deprecated. Use the packbeam_api module, instead. 46 | %% @end 47 | %%----------------------------------------------------------------------------- 48 | create(OutputPath, InputPaths, Prune, StartModule) -> 49 | io:format("WARNING. packbeam:create/4 is deprecated. Use packbeam_api::create/3 instead.~n"), 50 | packbeam_api:create(OutputPath, InputPaths, Prune, StartModule). 51 | 52 | %%----------------------------------------------------------------------------- 53 | %% @doc Deprecated. Use the packbeam_api module, instead. 54 | %% @end 55 | %%----------------------------------------------------------------------------- 56 | list(InputPath) -> 57 | io:format("WARNING. packbeam:list/1 is deprecated. Use packbeam_api::list/1 instead.~n"), 58 | packbeam_api:list(InputPath). 59 | 60 | %%----------------------------------------------------------------------------- 61 | %% @doc Deprecated. Use the packbeam_api module, instead. 62 | %% @end 63 | %%----------------------------------------------------------------------------- 64 | delete(OutputPath, InputPath, Names) -> 65 | io:format("WARNING. packbeam:delete/3 is deprecated. Use packbeam_api::delete/3 instead.~n"), 66 | packbeam_api:delete(OutputPath, InputPath, Names). 67 | 68 | %% 69 | %% escript entrypoint 70 | %% 71 | 72 | %% @hidden 73 | main(Argv) -> 74 | {Opts, Args} = parse_args(Argv), 75 | case length(Args) of 76 | 0 -> 77 | print_help(), 78 | erlang:halt(255); 79 | _ -> 80 | [Command | ArgsRest] = Args, 81 | try 82 | case Command of 83 | "create" -> 84 | erlang:halt(do_create(Opts, ArgsRest)); 85 | "list" -> 86 | erlang:halt(do_list(Opts, ArgsRest)); 87 | "extract" -> 88 | erlang:halt(do_extract(Opts, ArgsRest)); 89 | "delete" -> 90 | erlang:halt(do_delete(Opts, ArgsRest)); 91 | "version" -> 92 | io:format("~s~n", [get_version()]), 93 | erlang:halt(0); 94 | "help" -> 95 | print_help(), 96 | erlang:halt(0); 97 | _ -> 98 | io:format("packbeam: command must be one of create|list|delete|help~n"), 99 | print_help(), 100 | erlang:halt(255) 101 | end 102 | catch 103 | _:Exception:S -> 104 | io:format("packbeam: caught exception: ~p~n", [Exception]), 105 | io:format("Stacktrace: ~n~p~n", [S]), 106 | print_help(), 107 | erlang:halt(255) 108 | end 109 | end. 110 | 111 | %% 112 | %% escript internal functions 113 | %% 114 | 115 | %% @private 116 | print_help() -> 117 | io:format( 118 | "~n" 119 | "packbeam version ~s~n" 120 | "~n" 121 | "Syntax:~n" 122 | " packbeam ~n" 123 | "~n" 124 | "The following sub-commands are supported:~n" 125 | "~n" 126 | " create []+~n" 127 | " where:~n" 128 | " is the output AVM file,~n" 129 | " []+ is a list of one or more input files,~n" 130 | " and are among the following:~n" 131 | " [--prune|-p] Prune dependencies~n" 132 | " [--start|-s ] Start module~n" 133 | " [--remove_lines|-r] Remove line number information from AVM files~n" 134 | "~n" 135 | " list ~n" 136 | " where:~n" 137 | " is an AVM file,~n" 138 | " and are among the following:~n" 139 | " [--format|-f csv|bare|default] Format output~n" 140 | "~n" 141 | " extract []*~n" 142 | " where:~n" 143 | " is an AVM file,~n" 144 | " []+ is a list of one or more elements to extract~n" 145 | " (if empty, then extract all elements)~n" 146 | " and are among the following:~n" 147 | " [--out|-o ] Output directory into which to write elements~n" 148 | " (if unspecified, use the current working directory)~n" 149 | "~n" 150 | " delete []+~n" 151 | " where:~n" 152 | " is an AVM file,~n" 153 | " []+ is a list of one or more elements to delete,~n" 154 | " and are among the following:~n" 155 | " [--out|-o ] Output AVM file~n" 156 | "~n" 157 | " version~n" 158 | " Print version and exit~n" 159 | "~n" 160 | " help~n" 161 | " Print this help~n" 162 | "~n", 163 | [get_version()] 164 | ). 165 | 166 | %% @private 167 | get_version() -> 168 | case application:load(atomvm_packbeam) of 169 | ok -> 170 | case lists:keyfind(atomvm_packbeam, 1, application:loaded_applications()) of 171 | {_, _, Version} -> 172 | Version; 173 | false -> 174 | "Error! Unable to find atomvm_packbeam in loaded applications" 175 | end; 176 | {error, _Reason} -> 177 | "Error! Unable to load atomvm_packbeam application" 178 | end. 179 | 180 | %% @private 181 | do_create(Opts, Args) -> 182 | validate_args(create, Opts, Args), 183 | [OutputFile | InputFiles] = Args, 184 | ok = packbeam_api:create( 185 | OutputFile, 186 | InputFiles, 187 | #{ 188 | prune => maps:get(prune, Opts, false), 189 | start_module => maps:get(start_module, Opts, undefined), 190 | include_lines => not maps:get(remove_lines, Opts, false) 191 | } 192 | ), 193 | 0. 194 | 195 | %% @private 196 | do_list(Opts, Args) -> 197 | validate_args(list, Opts, Args), 198 | [InputFile | _] = Args, 199 | Modules = packbeam_api:list(InputFile), 200 | print_modules(Modules, maps:get(format, Opts, undefined)), 201 | 0. 202 | 203 | %% @private 204 | do_extract(Opts, Args) -> 205 | validate_args(extract, Opts, Args), 206 | [InputFile | Rest] = Args, 207 | OutputDir = maps:get(output, Opts, "."), 208 | ok = packbeam_api:extract(InputFile, Rest, OutputDir), 209 | 0. 210 | 211 | %% @private 212 | do_delete(Opts, Args) -> 213 | validate_args(delete, Opts, Args), 214 | [InputFile | _] = Args, 215 | OutputFile = maps:get(output, Opts, InputFile), 216 | packbeam_api:delete(OutputFile, InputFile, Args), 217 | 0. 218 | 219 | %% @private 220 | validate_args(create, _Opts, [OutputPath | _Rest] = _Args) -> 221 | case filelib:is_dir(OutputPath) of 222 | true -> 223 | throw(io_lib:format("Output file (~p) is a directory", [OutputPath])); 224 | _ -> 225 | ok 226 | end; 227 | validate_args(create, _Opts, [] = _Args) -> 228 | throw("Missing output file option"); 229 | %% 230 | validate_args(list, _Opts, [InputPath | _Rest] = _Args) -> 231 | case not filelib:is_file(InputPath) of 232 | true -> 233 | throw(io_lib:format("Input file (~p) does not exist", [InputPath])); 234 | _ -> 235 | ok 236 | end; 237 | validate_args(list, _Opts, [] = _Args) -> 238 | throw("Missing input option"); 239 | %% 240 | validate_args(extract, _Opts, [InputPath | _Rest] = _Args) -> 241 | case not filelib:is_file(InputPath) of 242 | true -> 243 | throw(io_lib:format("Input file (~p) does not exist", [InputPath])); 244 | _ -> 245 | ok 246 | end; 247 | validate_args(extract, _Opts, [] = _Args) -> 248 | throw("Missing input option"); 249 | %% 250 | validate_args(delete, _Opts, [InputPath | _Rest] = _Args) -> 251 | case not filelib:is_file(InputPath) of 252 | true -> 253 | throw(io_lib:format("Input file (~p) does not exist", [InputPath])); 254 | _ -> 255 | ok 256 | end; 257 | validate_args(delete, _Opts, [] = _Args) -> 258 | throw("Missing input option"). 259 | 260 | %% @private 261 | print_modules(Modules, "csv" = Format) -> 262 | io:format("MODULE_NAME,IS_BEAM,IS_ENTRYPOINT,SIZE_BYTES~n"), 263 | lists:foreach( 264 | fun(Module) -> print_module(Module, Format) end, 265 | Modules 266 | ); 267 | print_modules(Modules, Format) -> 268 | lists:foreach( 269 | fun(Module) -> print_module(Module, Format) end, 270 | Modules 271 | ). 272 | 273 | %% @private 274 | print_module(ParsedFile, undefined) -> 275 | print_module(ParsedFile, "default"); 276 | print_module(ParsedFile, "default") -> 277 | Name = packbeam_api:get_element_name(ParsedFile), 278 | Data = packbeam_api:get_element_data(ParsedFile), 279 | io:format( 280 | "~s~s [~p]~n", [ 281 | Name, 282 | case packbeam_api:is_entrypoint(ParsedFile) of 283 | true -> " *"; 284 | _ -> "" 285 | end, 286 | byte_size(Data) 287 | ] 288 | ); 289 | print_module(ParsedFile, "csv") -> 290 | Name = packbeam_api:get_element_name(ParsedFile), 291 | Data = packbeam_api:get_element_data(ParsedFile), 292 | io:format( 293 | "~s,~p,~p,~p~n", [ 294 | Name, 295 | packbeam_api:is_beam(ParsedFile), 296 | packbeam_api:is_entrypoint(ParsedFile), 297 | byte_size(Data) 298 | ] 299 | ); 300 | print_module(ParsedFile, "bare") -> 301 | Name = packbeam_api:get_element_name(ParsedFile), 302 | io:format( 303 | "~s~n", [ 304 | Name 305 | ] 306 | ); 307 | print_module(_ParsedFile, Format) -> 308 | throw({error, {unsupported_format, Format}}). 309 | 310 | %% @private 311 | parse_args(Argv) -> 312 | parse_args(Argv, {#{}, []}). 313 | 314 | %% @private 315 | parse_args([], {Opts, Args}) -> 316 | {Opts, lists:reverse(Args)}; 317 | parse_args(["-out", Path | T], {Opts, Args}) -> 318 | io:format("WARNING. Deprecated option. Use --out instead.~n"), 319 | parse_args(["--out", Path | T], {Opts, Args}); 320 | parse_args(["-o", Path | T], {Opts, Args}) -> 321 | parse_args(["--out", Path | T], {Opts, Args}); 322 | parse_args(["--out", Path | T], {Opts, Args}) -> 323 | parse_args(T, {Opts#{output => Path}, Args}); 324 | parse_args(["-prune" | T], {Opts, Args}) -> 325 | io:format("WARNING. Deprecated option. Use --prune instead.~n"), 326 | parse_args(["--prune" | T], {Opts, Args}); 327 | parse_args(["-p" | T], {Opts, Args}) -> 328 | parse_args(["--prune" | T], {Opts, Args}); 329 | parse_args(["--prune" | T], {Opts, Args}) -> 330 | parse_args(T, {Opts#{prune => true}, Args}); 331 | parse_args(["-start", Module | T], {Opts, Args}) -> 332 | io:format("WARNING. Deprecated option. Use --start instead.~n"), 333 | parse_args(["--start", Module | T], {Opts, Args}); 334 | parse_args(["-s", Module | T], {Opts, Args}) -> 335 | parse_args(["--start", Module | T], {Opts, Args}); 336 | parse_args(["--start", Module | T], {Opts, Args}) -> 337 | parse_args(T, {Opts#{start_module => list_to_atom(Module)}, Args}); 338 | parse_args(["-r" | T], {Opts, Args}) -> 339 | parse_args(["--remove_lines" | T], {Opts, Args}); 340 | parse_args(["--remove_lines" | T], {Opts, Args}) -> 341 | parse_args(T, {Opts#{remove_lines => true}, Args}); 342 | parse_args(["-format", Format | T], {Opts, Args}) -> 343 | io:format("WARNING. Deprecated option. Use --format instead.~n"), 344 | parse_args(["--format", Format | T], {Opts, Args}); 345 | parse_args(["-f", Format | T], {Opts, Args}) -> 346 | parse_args(["--format", Format | T], {Opts, Args}); 347 | parse_args(["--format", Format | T], {Opts, Args}) -> 348 | parse_args(T, {Opts#{format => Format}, Args}); 349 | parse_args([H | T], {Opts, Args}) -> 350 | parse_args(T, {Opts, [H | Args]}). 351 | -------------------------------------------------------------------------------- /src/packbeam_api.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% Licensed under the Apache License, Version 2.0 (the "License"); 6 | %% you may not use this file except in compliance with the License. 7 | %% You may obtain a copy of the License at 8 | %% 9 | %% http://www.apache.org/licenses/LICENSE-2.0 10 | %% 11 | %% Unless required by applicable law or agreed to in writing, software 12 | %% distributed under the License is distributed on an "AS IS" BASIS, 13 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | %% See the License for the specific language governing permissions and 15 | %% limitations under the License. 16 | %% 17 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 18 | 19 | %%----------------------------------------------------------------------------- 20 | %% @doc A library used to generate an 21 | %% AtomVM AVM file from a set of 22 | %% files (beam files, previously built AVM files, or even arbitrary data files). 23 | %% @end 24 | %%----------------------------------------------------------------------------- 25 | -module(packbeam_api). 26 | 27 | %% API exports 28 | -export([create/2, create/3, create/4, create/5, list/1, extract/3, delete/3]). 29 | 30 | %% AVM Entry functions 31 | -export([is_beam/1, is_entrypoint/1, get_element_name/1, get_element_data/1, get_element_module/1]). 32 | 33 | % erlfmt:ignore We want to keep the block format 34 | -define(AVM_HEADER, 35 | 16#23, 16#21, 16#2f, 16#75, 36 | 16#73, 16#72, 16#2f, 16#62, 37 | 16#69, 16#6e, 16#2f, 16#65, 38 | 16#6e, 16#76, 16#20, 16#41, 39 | 16#74, 16#6f, 16#6d, 16#56, 40 | 16#4d, 16#0a, 16#00, 16#00 41 | ). 42 | 43 | -define(ALLOWED_CHUNKS, [ 44 | "AtU8", "Code", "ExpT", "LocT", "ImpT", "LitU", "FunT", "StrT", "LitT" 45 | ]). 46 | -define(BEAM_START_FLAG, 1). 47 | -define(BEAM_CODE_FLAG, 2). 48 | -define(NORMAL_FILE_FLAG, 4). 49 | 50 | -opaque avm_element() :: [atom() | {atom(), term()}]. 51 | -type path() :: string(). 52 | -type avm_element_name() :: string(). 53 | -type options() :: #{ 54 | prune => boolean(), 55 | start_module => module() | undefined, 56 | application_module => module() | undefined, 57 | include_lines => boolean() 58 | }. 59 | 60 | -export_type([ 61 | path/0, 62 | avm_element/0, 63 | avm_element_name/0, 64 | options/0 65 | ]). 66 | 67 | -define(DEFAULT_OPTIONS, #{ 68 | prune => false, 69 | start_module => undefined, 70 | application_module => undefined, 71 | include_lines => true 72 | }). 73 | 74 | %% 75 | %% Public API 76 | %% 77 | 78 | %%----------------------------------------------------------------------------- 79 | %% @param OutputPath the path to write the AVM file 80 | %% @param InputPaths a list of paths of beam files, AVM files, or normal data files 81 | %% @returns ok if the file was created. 82 | %% @throws Reason::string() 83 | %% @doc Create an AVM file. 84 | %% 85 | %% Equivalent to `create(OutputPath, InputPaths, DefaultOptions)' 86 | %% 87 | %% where `DefaultOptions' is `#{ 88 | %% prune => false, 89 | %% start_module => undefined, 90 | %% application_module => undefined, 91 | %% include_lines => false 92 | %% }' 93 | %% 94 | %% @end 95 | %%----------------------------------------------------------------------------- 96 | -spec create( 97 | OutputPath :: path(), 98 | InputPaths :: [path()] 99 | ) -> ok | {error, Reason :: term()}. 100 | create(OutputPath, InputPaths) -> 101 | create(OutputPath, InputPaths, ?DEFAULT_OPTIONS). 102 | 103 | %%----------------------------------------------------------------------------- 104 | %% @param OutputPath the path to write the AVM file 105 | %% @param InputPaths a list of paths of beam files, AVM files, or normal data files 106 | %% @param Options creation options 107 | %% @returns ok if the file was created. 108 | %% @throws Reason::string() 109 | %% @doc Create an AVM file. 110 | %% 111 | %% This function will create an AVM file at the location specified in 112 | %% OutputPath, using the input files specified in InputPaths. 113 | %% @end 114 | %%----------------------------------------------------------------------------- 115 | -spec create( 116 | OutputPath :: path(), 117 | InputPaths :: [path()], 118 | Options :: options() 119 | ) -> ok | {error, Reason :: term()}. 120 | create(OutputPath, InputPaths, Options) -> 121 | #{ 122 | prune := Prune, 123 | start_module := StartModule, 124 | application_module := ApplicationModule, 125 | include_lines := IncludeLines 126 | } = maps:merge(?DEFAULT_OPTIONS, Options), 127 | ParsedFiles = parse_files(InputPaths, StartModule, IncludeLines), 128 | write_packbeam( 129 | OutputPath, 130 | case Prune of 131 | true -> prune(ParsedFiles, ApplicationModule); 132 | _ -> ParsedFiles 133 | end 134 | ). 135 | 136 | %%----------------------------------------------------------------------------- 137 | %% @param OutputPath the path to write the AVM file 138 | %% @param InputPaths a list of paths of beam files, AVM files, or normal data files 139 | %% @param Prune whether to prune the archive. Without pruning, all found BEAM files are included 140 | %% in the output AVM file. With pruning, then packbeam will attempt to 141 | %% determine which BEAM files are needed to run the application, depending 142 | %% on which modules are (transitively) referenced from the AVM entrypoint. 143 | %% @param StartModule if `undefined', then this parameter is a module that 144 | %% is intended to the the start module for the application. This module 145 | %% will occur first in the generated AVM file. 146 | %% @returns ok if the file was created. 147 | %% @throws Reason::string() 148 | %% @deprecated This function is deprecated. Use `create/3' instead. 149 | %% @doc Create an AVM file. 150 | %% 151 | %% Equivalent to create(OutputPath, InputPaths, undefined, Prune, StartModule). 152 | %% @end 153 | %% @hidden 154 | %%----------------------------------------------------------------------------- 155 | -spec create( 156 | OutputPath :: path(), 157 | InputPaths :: [path()], 158 | Prune :: boolean(), 159 | StartModule :: module() | undefined 160 | ) -> 161 | ok | {error, Reason :: term()}. 162 | create(OutputPath, InputPaths, Prune, StartModule) -> 163 | io:format("WARNING: Deprecated function: ~p:create/4~n", [?MODULE]), 164 | Options = #{prune => Prune, start_module => StartModule}, 165 | create(OutputPath, InputPaths, maps:merge(?DEFAULT_OPTIONS, Options)). 166 | 167 | %%----------------------------------------------------------------------------- 168 | %% @param OutputPath the path to write the AVM file 169 | %% @param InputPaths a list of paths of beam files, AVM files, or normal data files 170 | %% @param ApplicationModule If not `undefined', then this parameter designates 171 | %% the name of an OTP application, from which additional dependencies 172 | %% will be computed. 173 | %% @param Prune whether to prune the archive. Without pruning, all found BEAM files are included 174 | %% in the output AVM file. With pruning, then packbeam will attempt to 175 | %% determine which BEAM files are needed to run the application, depending 176 | %% on which modules are (transitively) referenced from the AVM entrypoint. 177 | %% @param StartModule if `undefined', then this parameter is a module that 178 | %% is intended to the the start module for the application. This module 179 | %% will occur first in the generated AVM file. 180 | %% @returns ok if the file was created. 181 | %% @throws Reason::string() 182 | %% @deprecated This function is deprecated. Use `create/3' instead. 183 | %% @doc Create an AVM file. 184 | %% 185 | %% This function will create an AVM file at the location specified in 186 | %% OutputPath, using the input files specified in InputPaths. 187 | %% @end 188 | %% @hidden 189 | %%----------------------------------------------------------------------------- 190 | -spec create( 191 | OutputPath :: path(), 192 | InputPaths :: [path()], 193 | ApplicationModule :: module() | undefined, 194 | Prune :: boolean(), 195 | StartModule :: module() | undefined 196 | ) -> 197 | ok | {error, Reason :: term()}. 198 | create(OutputPath, InputPaths, ApplicationModule, Prune, StartModule) -> 199 | io:format("WARNING: Deprecated function: ~p:create/5~n", [?MODULE]), 200 | Options = #{ 201 | prune => Prune, start_module => StartModule, application_module => ApplicationModule 202 | }, 203 | create(OutputPath, InputPaths, maps:merge(?DEFAULT_OPTIONS, Options)). 204 | 205 | %%----------------------------------------------------------------------------- 206 | %% @param InputPath the AVM file from which to list elements 207 | %% @returns list of element data 208 | %% @throws Reason::string() 209 | %% @doc List the contents of an AVM file. 210 | %% 211 | %% This function will list the contents of an AVM file at the 212 | %% location specified in InputPath. 213 | %% @end 214 | %%----------------------------------------------------------------------------- 215 | -spec list(InputPath :: path()) -> [avm_element()]. 216 | list(InputPath) -> 217 | case file_type(InputPath) of 218 | avm -> 219 | parse_file(InputPath, false); 220 | _ -> 221 | throw(io_lib:format("Expected AVM file: ~p", [InputPath])) 222 | end. 223 | 224 | %%----------------------------------------------------------------------------- 225 | %% @param InputPath the AVM file from which to extract elements 226 | %% @param AVMElementNames a list of elements from the source AVM file to extract. If 227 | %% empty, then extract all elements. 228 | %% @param OutputDir the directory to write the contents 229 | %% @returns ok if the file was created. 230 | %% @throws Reason::string() 231 | %% @doc Extract all or selected elements from an AVM file. 232 | %% 233 | %% This function will extract elements of an AVM file at the location specified in 234 | %% InputPath, specified by the supplied list of names. The elements 235 | %% from the input AVM file will be written into the specified output directory, 236 | %% creating any subdirectories if the AVM file elements contain path information. 237 | %% @end 238 | %%----------------------------------------------------------------------------- 239 | -spec extract( 240 | InputPath :: path(), 241 | AVMElementNames :: [avm_element_name()], 242 | OutputDir :: path() 243 | ) -> ok | {error, Reason :: term()}. 244 | extract(InputPath, AVMElementNames, OutputDir) -> 245 | case file_type(InputPath) of 246 | avm -> 247 | ParsedFiles = parse_file(InputPath, false), 248 | write_files(filter_names(AVMElementNames, ParsedFiles), OutputDir); 249 | _ -> 250 | throw(io_lib:format("Expected AVM file: ~p", [InputPath])) 251 | end. 252 | 253 | %%----------------------------------------------------------------------------- 254 | %% @param InputPath the AVM file from which to delete elements 255 | %% @param OutputPath the path to write the AVM file 256 | %% @param AVMElementNames a list of elements from the source AVM file to delete 257 | %% @returns ok if the file was created. 258 | %% @throws Reason::string() 259 | %% @doc Delete selected elements of an AVM file. 260 | %% 261 | %% This function will delete elements of an AVM file at the location specified in 262 | %% InputPath, specified by the supplied list of names. The output AVM file 263 | %% is written to OutputPath, which may be the same as InputPath. 264 | %% @end 265 | %%----------------------------------------------------------------------------- 266 | -spec delete( 267 | OutputPath :: path(), 268 | InputPath :: path(), 269 | AVMElementNames :: [avm_element_name()] 270 | ) -> ok | {error, Reason :: term()}. 271 | delete(OutputPath, InputPath, AVMElementNames) -> 272 | case file_type(InputPath) of 273 | avm -> 274 | ParsedFiles = parse_file(InputPath, false), 275 | write_packbeam(OutputPath, remove_names(AVMElementNames, ParsedFiles)); 276 | _ -> 277 | throw(io_lib:format("Expected AVM file: ~p", [InputPath])) 278 | end. 279 | 280 | %%----------------------------------------------------------------------------- 281 | %% @param AVMElement An AVM file element 282 | %% @returns the name of the element. 283 | %% @doc Return the name of the element. 284 | %% @end 285 | %%----------------------------------------------------------------------------- 286 | -spec get_element_name(AVMElement :: avm_element()) -> avm_element_name(). 287 | get_element_name(AVMElement) -> 288 | proplists:get_value(element_name, AVMElement). 289 | 290 | %%----------------------------------------------------------------------------- 291 | %% @param AVMElement An AVM file element 292 | %% @returns the AVM element data. 293 | %% @doc Return AVM element data. 294 | %% @end 295 | %%----------------------------------------------------------------------------- 296 | -spec get_element_data(AVMElement :: avm_element()) -> binary(). 297 | get_element_data(AVMElement) -> 298 | proplists:get_value(data, AVMElement). 299 | 300 | %%----------------------------------------------------------------------------- 301 | %% @param AVMElement An AVM file element 302 | %% @returns the AVM element module, if the element is a BEAM file; `undefined', 303 | %% otherwise. 304 | %% @doc Return AVM element module, if the element is a BEAM file. 305 | %% @end 306 | %%----------------------------------------------------------------------------- 307 | -spec get_element_module(AVMElement :: avm_element()) -> module() | undefined. 308 | get_element_module(AVMElement) -> 309 | proplists:get_value(module, AVMElement). 310 | 311 | %%----------------------------------------------------------------------------- 312 | %% @param AVMElement An AVM file element 313 | %% @returns `true' if the AVM element is an entrypoint (i.e., exports a `start/0' 314 | %% function); `false' otherwise. 315 | %% @doc Indicates whether the AVM file element is an entrypoint. 316 | %% @end 317 | %%----------------------------------------------------------------------------- 318 | -spec is_entrypoint(AVMElement :: avm_element()) -> boolean(). 319 | is_entrypoint(AVMElement) -> 320 | (get_flags(AVMElement) band ?BEAM_START_FLAG) =:= ?BEAM_START_FLAG. 321 | 322 | %%----------------------------------------------------------------------------- 323 | %% @param AVMElement An AVM file element 324 | %% @returns `true' if the AVM element is a BEAM file; `false' otherwise. 325 | %% @doc Indicates whether the AVM file element is a BEAM file. 326 | %% @end 327 | %%----------------------------------------------------------------------------- 328 | -spec is_beam(AVMElement :: avm_element()) -> boolean(). 329 | is_beam(AVMElement) -> 330 | (get_flags(AVMElement) band ?BEAM_CODE_FLAG) =:= ?BEAM_CODE_FLAG. 331 | 332 | %% 333 | %% Internal API functions 334 | %% 335 | 336 | %% @private 337 | get_flags(AVMElement) -> 338 | proplists:get_value(flags, AVMElement). 339 | 340 | %% @private 341 | parse_files(InputPaths, StartModule, IncludeLines) -> 342 | Files = lists:foldl( 343 | fun(InputPath, Accum) -> 344 | Accum ++ parse_file(InputPath, IncludeLines) 345 | end, 346 | [], 347 | InputPaths 348 | ), 349 | case StartModule of 350 | undefined -> 351 | Files; 352 | _ -> 353 | reorder_start_module(StartModule, Files) 354 | end. 355 | 356 | %% @private 357 | parse_file({InputPath, ModuleName}, IncludeLines) -> 358 | parse_file(file_type(InputPath), ModuleName, load_file(InputPath), IncludeLines); 359 | parse_file(InputPath, IncludeLines) -> 360 | parse_file(file_type(InputPath), InputPath, load_file(InputPath), IncludeLines). 361 | 362 | %% @private 363 | file_type(InputPath) -> 364 | case ends_with(InputPath, ".beam") of 365 | true -> 366 | beam; 367 | _ -> 368 | case ends_with(InputPath, ".avm") of 369 | true -> avm; 370 | _ -> normal 371 | end 372 | end. 373 | 374 | %% @private 375 | load_file(Path) -> 376 | case file:read_file(Path) of 377 | {ok, Data} -> 378 | Data; 379 | {error, Reason} -> 380 | throw(io_lib:format("Unable to load file ~s. Reason: ~p", [Path, Reason])) 381 | end. 382 | 383 | %% @private 384 | prune(ParsedFiles, RootApplicationModule) -> 385 | case find_entrypoint(ParsedFiles) of 386 | false -> 387 | throw("No input beam files contain start/0 entrypoint"); 388 | {value, Entrypoint} -> 389 | BeamFiles = lists:filter(fun is_beam/1, ParsedFiles), 390 | Modules = closure(Entrypoint, BeamFiles, [get_element_module(Entrypoint)]), 391 | ApplicationStartModules = find_application_modules(ParsedFiles, RootApplicationModule), 392 | ApplicationModules = find_dependencies(ApplicationStartModules, BeamFiles), 393 | filter_modules(Modules ++ ApplicationModules, ParsedFiles) 394 | end. 395 | 396 | find_application_modules(_ParsedFiles, undefined) -> 397 | []; 398 | find_application_modules(ParsedFiles, ApplicationModule) -> 399 | ApplicationSpecs = find_all_application_specs(ParsedFiles), 400 | find_application_start_modules(ParsedFiles, ApplicationSpecs, ApplicationModule). 401 | 402 | find_all_application_specs(ParsedFiles) -> 403 | ApplicationFiles = lists:filter( 404 | fun is_application_file/1, 405 | ParsedFiles 406 | ), 407 | [get_application_spec(ApplicationFile) || ApplicationFile <- ApplicationFiles]. 408 | 409 | find_application_start_modules(ParsedFiles, ApplicationSpecs, ApplicationModule) -> 410 | case find_application_spec(ApplicationSpecs, ApplicationModule) of 411 | false -> 412 | []; 413 | {value, {application, _ApplicationModule, Properties}} -> 414 | ChildApplicationStartModules = 415 | case proplists:get_value(applications, Properties) of 416 | Applications when is_list(Applications) -> 417 | lists:foldl( 418 | fun(Application, InnerAccum) -> 419 | find_application_start_modules( 420 | ParsedFiles, ApplicationSpecs, Application 421 | ) ++ InnerAccum 422 | end, 423 | [], 424 | Applications 425 | ); 426 | _ -> 427 | [] 428 | end, 429 | StartModules = 430 | case proplists:get_value(mod, Properties) of 431 | {StartModule, _Args} when is_atom(StartModule) -> 432 | [StartModule]; 433 | _ -> 434 | [] 435 | end, 436 | ChildApplicationStartModules ++ StartModules 437 | end. 438 | 439 | find_dependencies(Entrypoints, BeamFiles) -> 440 | deduplicate( 441 | lists:flatten( 442 | [ 443 | closure( 444 | get_parsed_file(Entrypoint, BeamFiles), 445 | BeamFiles, 446 | [Entrypoint] 447 | ) 448 | || Entrypoint <- Entrypoints 449 | ] 450 | ) 451 | ). 452 | 453 | find_application_spec(ApplicationSpecs, ApplicationModule) -> 454 | lists:search( 455 | fun({application, Module, _Properties}) -> 456 | Module =:= ApplicationModule 457 | end, 458 | ApplicationSpecs 459 | ). 460 | 461 | get_application_spec(ApplicationFile) -> 462 | ApplicationData = get_element_data(ApplicationFile), 463 | <<_Size:4/binary, SerializedTerm/binary>> = ApplicationData, 464 | binary_to_term(SerializedTerm). 465 | 466 | is_application_file(ParsedFile) -> 467 | case not is_beam(ParsedFile) of 468 | true -> 469 | ModuleName = get_element_name(ParsedFile), 470 | Components = string:split(ModuleName, "/", all), 471 | case Components of 472 | [_ModuleName, "priv", "application.bin"] -> 473 | true; 474 | _ -> 475 | false 476 | end; 477 | false -> 478 | false 479 | end. 480 | 481 | %% @private 482 | find_entrypoint(ParsedFiles) -> 483 | lists:search(fun is_entrypoint/1, ParsedFiles). 484 | 485 | %% @private 486 | closure(_Current, [], Accum) -> 487 | lists:reverse(Accum); 488 | closure(Current, Candidates, Accum) -> 489 | CandidateModules = get_element_modules(Candidates), 490 | CurrentsImports = get_imports(Current), 491 | CurrentsAtoms = get_atoms(Current), 492 | DepModules = intersection(CurrentsImports ++ CurrentsAtoms, CandidateModules) -- Accum, 493 | lists:foldl( 494 | fun(DepModule, A) -> 495 | case lists:member(DepModule, A) of 496 | true -> 497 | A; 498 | _ -> 499 | NewCandidates = remove(DepModule, Candidates), 500 | closure(get_parsed_file(DepModule, Candidates), NewCandidates, [DepModule | A]) 501 | end 502 | end, 503 | Accum, 504 | DepModules 505 | ). 506 | 507 | %% @private 508 | remove(Module, ParsedFiles) -> 509 | [P || P <- ParsedFiles, Module =/= get_element_module(P)]. 510 | 511 | %% @private 512 | get_imports(ParsedFile) -> 513 | Imports = proplists:get_value(imports, proplists:get_value(chunk_refs, ParsedFile)), 514 | lists:usort([M || {M, _F, _A} <- Imports]). 515 | 516 | %% @private 517 | get_atoms(ParsedFile) -> 518 | AtomsT = [ 519 | Atom 520 | || {_Index, Atom} <- proplists:get_value(atoms, proplists:get_value(chunk_refs, ParsedFile)) 521 | ], 522 | AtomsFromLiterals = get_atom_literals(proplists:get_value(uncompressed_literals, ParsedFile)), 523 | lists:usort(AtomsT ++ AtomsFromLiterals). 524 | 525 | %% @private 526 | get_atom_literals(undefined) -> 527 | []; 528 | get_atom_literals(UncompressedLiterals) -> 529 | <> = UncompressedLiterals, 530 | get_atom_literals(NumLiterals, Rest, []). 531 | 532 | %% @private 533 | get_atom_literals(0, <<"">>, Accum) -> 534 | Accum; 535 | get_atom_literals(I, Data, Accum) -> 536 | <> = Data, 537 | <> = StartData, 538 | Literal = binary_to_term(EncodedLiteral), 539 | ExtractedAtoms = extract_atoms(Literal, []), 540 | get_atom_literals(I - 1, Rest, ExtractedAtoms ++ Accum). 541 | 542 | %% @private 543 | extract_atoms(Term, Accum) when is_atom(Term) -> 544 | [Term | Accum]; 545 | extract_atoms(Term, Accum) when is_tuple(Term) -> 546 | extract_atoms(tuple_to_list(Term), Accum); 547 | extract_atoms(Term, Accum) when is_map(Term) -> 548 | extract_atoms(maps:to_list(Term), Accum); 549 | extract_atoms([H | T], Accum) -> 550 | HeadAtoms = extract_atoms(H, []), 551 | extract_atoms(T, HeadAtoms ++ Accum); 552 | extract_atoms(_Term, Accum) -> 553 | Accum. 554 | 555 | %% @private 556 | get_element_modules(ParsedFiles) -> 557 | [get_element_module(ParsedFile) || ParsedFile <- ParsedFiles]. 558 | 559 | %% @private 560 | get_parsed_file(Module, ParsedFiles) -> 561 | SearchResult = lists:search( 562 | fun(ParsedFile) -> 563 | get_element_module(ParsedFile) =:= Module 564 | end, 565 | ParsedFiles 566 | ), 567 | case SearchResult of 568 | {value, V} -> 569 | V; 570 | false -> 571 | exit({module_not_found, Module, ParsedFiles}) 572 | end. 573 | 574 | %% @private 575 | intersection(A, B) -> 576 | lists:filter( 577 | fun(E) -> 578 | lists:member(E, B) 579 | end, 580 | A 581 | ). 582 | 583 | %% @private 584 | filter_modules(Modules, ParsedFiles) -> 585 | lists:filter( 586 | fun(ParsedFile) -> 587 | case is_beam(ParsedFile) of 588 | true -> 589 | lists:member(get_element_module(ParsedFile), Modules); 590 | _ -> 591 | true 592 | end 593 | end, 594 | ParsedFiles 595 | ). 596 | 597 | %% @private 598 | parse_file(beam, _ModuleName, Data, IncludeLines) -> 599 | {ok, Module, Chunks} = beam_lib:all_chunks(Data), 600 | {UncompressedChunks, UncompressedLiterals} = maybe_uncompress_literals(Chunks), 601 | FilteredChunks = filter_chunks(UncompressedChunks, IncludeLines), 602 | {ok, Binary} = beam_lib:build_module(FilteredChunks), 603 | {ok, {Module, ChunkRefs}} = beam_lib:chunks(Data, [imports, exports, atoms]), 604 | Exports = proplists:get_value(exports, ChunkRefs), 605 | Flags = 606 | case lists:member({start, 0}, Exports) of 607 | true -> 608 | ?BEAM_CODE_FLAG bor ?BEAM_START_FLAG; 609 | _ -> 610 | ?BEAM_CODE_FLAG 611 | end, 612 | [ 613 | [ 614 | {module, Module}, 615 | {element_name, io_lib:format("~s.beam", [atom_to_list(Module)])}, 616 | {flags, Flags}, 617 | {data, Binary}, 618 | {chunk_refs, ChunkRefs}, 619 | {uncompressed_literals, UncompressedLiterals} 620 | ] 621 | ]; 622 | parse_file(avm, ModuleName, Data, _IncludeLines) -> 623 | case Data of 624 | <> -> 625 | parse_avm_data(AVMData); 626 | _ -> 627 | throw(io_lib:format("Invalid AVM header: ~p", [ModuleName])) 628 | end; 629 | parse_file(normal, ModuleName, Data, _IncludeLines) -> 630 | DataSize = byte_size(Data), 631 | Blob = <>, 632 | [[{element_name, ModuleName}, {flags, ?NORMAL_FILE_FLAG}, {data, Blob}]]. 633 | 634 | %% @private 635 | reorder_start_module(StartModule, Files) -> 636 | {StartProps, Other} = 637 | lists:partition( 638 | fun(Props) -> 639 | % io:format("Props: ~w~n", [Props]), 640 | case get_element_module(Props) of 641 | StartModule -> 642 | case is_entrypoint(Props) of 643 | true -> true; 644 | _ -> throw({start_module_not_start_beam, StartModule}) 645 | end; 646 | _ -> 647 | false 648 | end 649 | end, 650 | Files 651 | ), 652 | case StartProps of 653 | [] -> 654 | throw({start_module_not_found, StartModule}); 655 | _ -> 656 | StartProps ++ Other 657 | end. 658 | 659 | %% @private 660 | parse_avm_data(AVMData) -> 661 | parse_avm_data(AVMData, []). 662 | 663 | %% @private 664 | parse_avm_data(<<"">>, Accum) -> 665 | lists:reverse(Accum); 666 | parse_avm_data(<<0:32/integer, _AVMRest/binary>>, Accum) -> 667 | lists:reverse(Accum); 668 | parse_avm_data(<>, Accum) -> 669 | SizeWithoutSizeField = Size - 4, 670 | case SizeWithoutSizeField =< erlang:byte_size(AVMRest) of 671 | true -> 672 | <> = AVMRest, 673 | Beam = parse_beam(AVMBeamData, [], in_header, 0, []), 674 | parse_avm_data(AVMNext, [Beam | Accum]); 675 | _ -> 676 | throw( 677 | io_lib:format("Invalid AVM data size: ~p (AVMRest=~p)", [ 678 | Size, erlang:byte_size(AVMRest) 679 | ]) 680 | ) 681 | end. 682 | 683 | %% @private 684 | parse_beam(<>, [], in_header, Pos, Accum) -> 685 | parse_beam(Rest, [], in_element_name, Pos + 8, [{flags, Flags} | Accum]); 686 | parse_beam(<<0:8, Rest/binary>>, Tmp, in_element_name, Pos, Accum) -> 687 | ModuleName = lists:reverse(Tmp), 688 | case (Pos + 1) rem 4 of 689 | 0 -> 690 | parse_beam(Rest, Tmp, in_data, Pos, [{element_name, ModuleName} | Accum]); 691 | _ -> 692 | parse_beam(Rest, [], eat_padding, Pos + 1, [{element_name, ModuleName} | Accum]) 693 | end; 694 | parse_beam(<>, Tmp, in_element_name, Pos, Accum) -> 695 | parse_beam(Rest, [C | Tmp], in_element_name, Pos + 1, Accum); 696 | parse_beam(<<0:8, Rest/binary>>, Tmp, eat_padding, Pos, Accum) -> 697 | case (Pos + 1) rem 4 of 698 | 0 -> 699 | parse_beam(Rest, Tmp, in_data, Pos, Accum); 700 | _ -> 701 | parse_beam(Rest, Tmp, eat_padding, Pos + 1, Accum) 702 | end; 703 | parse_beam(Bin, Tmp, eat_padding, Pos, Accum) -> 704 | parse_beam(Bin, Tmp, in_data, Pos, Accum); 705 | parse_beam(Data, _Tmp, in_data, _Pos, Accum) -> 706 | case is_beam(Accum) orelse is_entrypoint(Accum) of 707 | true -> 708 | StrippedData = strip_padding(Data), 709 | {ok, {Module, ChunkRefs}} = beam_lib:chunks(StrippedData, [imports, exports, atoms]), 710 | [{module, Module}, {chunk_refs, ChunkRefs}, {data, StrippedData} | Accum]; 711 | _ -> 712 | [{data, Data} | Accum] 713 | end. 714 | 715 | strip_padding(<<0:8, Rest/binary>>) -> 716 | strip_padding(Rest); 717 | strip_padding(Data) -> 718 | Data. 719 | 720 | %% @private 721 | maybe_uncompress_literals(Chunks) -> 722 | case proplists:get_value("LitT", Chunks) of 723 | undefined -> 724 | {Chunks, undefined}; 725 | <<0:32, Data/binary>> -> 726 | {Chunks, Data}; 727 | <<_Size:4/binary, Data/binary>> -> 728 | do_uncompress_literals(Chunks, Data) 729 | end. 730 | 731 | %% @private 732 | do_uncompress_literals(Chunks, Data) -> 733 | UncompressedData = zlib:uncompress(Data), 734 | { 735 | lists:keyreplace( 736 | "LitT", 737 | 1, 738 | Chunks, 739 | {"LitU", UncompressedData} 740 | ), 741 | UncompressedData 742 | }. 743 | 744 | %% @private 745 | write_packbeam(OutputFilePath, ParsedFiles) -> 746 | PackedData = 747 | [<> | [pack_data(ParsedFile) || ParsedFile <- ParsedFiles]] ++ 748 | [create_header(0, 0, <<"end">>)], 749 | file:write_file(OutputFilePath, PackedData). 750 | 751 | %% @private 752 | pack_data(ParsedFile) -> 753 | ModuleName = list_to_binary(get_element_name(ParsedFile)), 754 | Flags = get_flags(ParsedFile), 755 | Data = get_element_data(ParsedFile), 756 | HeaderSize = header_size(ModuleName), 757 | HeaderPadding = create_padding(HeaderSize), 758 | DataSize = byte_size(Data), 759 | DataPadding = create_padding(DataSize), 760 | Size = HeaderSize + byte_size(HeaderPadding) + DataSize + byte_size(DataPadding), 761 | Header = create_header(Size, Flags, ModuleName), 762 | <
>. 763 | 764 | %% @private 765 | header_size(ModuleName) -> 766 | 4 + 4 + 4 + byte_size(ModuleName) + 1. 767 | 768 | %% @private 769 | create_header(Size, Flags, ModuleName) -> 770 | <>. 771 | 772 | %% @private 773 | create_padding(Size) -> 774 | case Size rem 4 of 775 | 0 -> <<"">>; 776 | K -> list_to_binary(lists:duplicate(4 - K, 0)) 777 | end. 778 | 779 | %% @private 780 | ends_with(String, Suffix) -> 781 | string:find(String, Suffix, trailing) =:= Suffix. 782 | 783 | %% @private 784 | filter_chunks(Chunks, IncludeLines) -> 785 | AllowedChunks = allowed_chunks(IncludeLines), 786 | lists:filter( 787 | fun({Tag, _Data}) -> lists:member(Tag, AllowedChunks) end, 788 | Chunks 789 | ). 790 | 791 | allowed_chunks(false) -> 792 | ?ALLOWED_CHUNKS; 793 | allowed_chunks(true) -> 794 | ["Line" | ?ALLOWED_CHUNKS]. 795 | 796 | %% @private 797 | remove_names(Names, ParsedFiles) -> 798 | lists:filter( 799 | fun(ParsedFile) -> 800 | ModuleName = get_element_name(ParsedFile), 801 | not lists:member(ModuleName, Names) 802 | end, 803 | ParsedFiles 804 | ). 805 | 806 | %% @private 807 | write_files(ParsedFiles, OutputDir) -> 808 | case filelib:is_dir(OutputDir) of 809 | true -> 810 | io:format("Writing to ~s ...~n", [OutputDir]), 811 | lists:foreach( 812 | fun(ParsedFile) -> 813 | ModuleName = get_element_name(ParsedFile), 814 | Path = OutputDir ++ "/" ++ ModuleName, 815 | case filelib:ensure_dir(Path) of 816 | ok -> 817 | io:format("x ~s~n", [ModuleName]), 818 | RawData = get_element_data(ParsedFile), 819 | Data = 820 | case file_type(ModuleName) of 821 | normal -> 822 | <> = RawData, 823 | <> = Rest, 824 | Contents; 825 | _ -> 826 | RawData 827 | end, 828 | file:write_file(Path, Data); 829 | Error -> 830 | throw(Error) 831 | end 832 | end, 833 | ParsedFiles 834 | ); 835 | _ -> 836 | throw(enoent) 837 | end. 838 | 839 | %% @private 840 | filter_names([], ParsedFiles) -> 841 | ParsedFiles; 842 | filter_names(Names, ParsedFiles) -> 843 | lists:filter( 844 | fun(ParsedFile) -> 845 | ModuleName = get_element_name(ParsedFile), 846 | lists:member(ModuleName, Names) 847 | end, 848 | ParsedFiles 849 | ). 850 | 851 | %% @private 852 | deduplicate([]) -> 853 | []; 854 | deduplicate([H | T]) -> 855 | [H | [X || X <- deduplicate(T), X /= H]]. 856 | -------------------------------------------------------------------------------- /test/a.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(a). 8 | 9 | -export([start/0]). 10 | 11 | start() -> 12 | b:start(). 13 | -------------------------------------------------------------------------------- /test/b.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(b). 8 | 9 | -export([start/0]). 10 | 11 | start() -> 12 | e:b_calls_me(), 13 | Module = get_module(), 14 | Module:test(). 15 | 16 | get_module() -> 17 | c. 18 | -------------------------------------------------------------------------------- /test/c.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(c). 8 | 9 | -export([test/0]). 10 | 11 | test() -> 12 | Literal = get_literal(), 13 | Module = maps:get(module, Literal), 14 | Module:c_calls_me(). 15 | 16 | get_literal() -> 17 | #{ 18 | module => f 19 | }. 20 | -------------------------------------------------------------------------------- /test/d.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(d). 8 | 9 | -export([no_one_calls_me/0]). 10 | 11 | no_one_calls_me() -> 12 | sorry. 13 | -------------------------------------------------------------------------------- /test/e.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(e). 8 | 9 | -export([b_calls_me/0]). 10 | 11 | b_calls_me() -> 12 | ok. 13 | -------------------------------------------------------------------------------- /test/f.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(f). 8 | 9 | -export([c_calls_me/0]). 10 | 11 | c_calls_me() -> 12 | ok. 13 | -------------------------------------------------------------------------------- /test/priv/test.txt: -------------------------------------------------------------------------------- 1 | this is a test 2 | -------------------------------------------------------------------------------- /test/priv/test.txt.license: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Fred Dushin 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 4 | -------------------------------------------------------------------------------- /test/prop_packbeam.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% Licensed under the Apache License, Version 2.0 (the "License"); 6 | %% you may not use this file except in compliance with the License. 7 | %% You may obtain a copy of the License at 8 | %% 9 | %% http://www.apache.org/licenses/LICENSE-2.0 10 | %% 11 | %% Unless required by applicable law or agreed to in writing, software 12 | %% distributed under the License is distributed on an "AS IS" BASIS, 13 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | %% See the License for the specific language governing permissions and 15 | %% limitations under the License. 16 | %% 17 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 18 | 19 | -module(prop_packbeam). 20 | -include_lib("proper/include/proper.hrl"). 21 | 22 | -define(BUILD_DIR, "_build/"). 23 | -define(TEST_BEAM_DIR, "_build/test/lib/atomvm_packbeam/test/"). 24 | 25 | %% 26 | %% Call graph for modules a-f, x 27 | %% 28 | %% 29 | %% calls calls 30 | %% a (start) ----------> b (start) ---------> e 31 | %% | 32 | %% | calls (via atomic module name) 33 | %% | 34 | %% v 35 | %% c -----------------> f 36 | %% calls (via atom inside of literal) 37 | %% 38 | %% 39 | %% d no one calls d :( 40 | %% 41 | %% 42 | %% x (start) ---> a (start) 43 | %% 44 | 45 | %% 46 | %% Properties 47 | %% 48 | 49 | prop_simple_test() -> 50 | ?FORALL( 51 | ModulePaths, 52 | module_paths(), 53 | begin 54 | Modules = [Module || {Module, _Path} <- ModulePaths], 55 | Paths = [Path || {_Module, Path} <- ModulePaths], 56 | AVMFile = dest_dir("prop_simple_test.avm"), 57 | collect( 58 | Modules, 59 | begin 60 | ok = packbeam_api:create(AVMFile, Paths), 61 | ParsedFiles = packbeam_api:list(AVMFile), 62 | modules_and_parsed_files_are_equivalent(Modules, ParsedFiles) andalso 63 | all_beam_modules_are_properly_named(ParsedFiles) andalso 64 | maybe_contains_start_beam(Modules, a, ParsedFiles) andalso 65 | maybe_contains_start_beam(Modules, b, ParsedFiles) 66 | end 67 | ) 68 | end 69 | ). 70 | 71 | modules_and_parsed_files_are_equivalent(Modules, ParsedFiles) -> 72 | lists:sort(Modules) =:= 73 | lists:sort([get_module(ParsedFile) || ParsedFile <- get_beam_files(ParsedFiles)]). 74 | 75 | all_beam_modules_are_properly_named(ParsedFiles) -> 76 | lists:all( 77 | fun(ParsedFile) -> 78 | Module = get_module(ParsedFile), 79 | get_module_name(ParsedFile) =:= (atom_to_list(Module) ++ ".beam") 80 | end, 81 | get_beam_files(ParsedFiles) 82 | ). 83 | 84 | get_beam_files(ParsedFiles) -> 85 | [ParsedFile || ParsedFile <- ParsedFiles, is_beam(ParsedFile)]. 86 | 87 | maybe_contains_start_beam(Modules, Module, ParsedFiles) -> 88 | case lists:member(Module, Modules) of 89 | true -> 90 | is_start(find_parsed_file(Module, ParsedFiles)); 91 | _ -> 92 | %% ignore 93 | true 94 | end. 95 | 96 | find_parsed_file(Module, []) -> 97 | {parsed_file_not_found, Module}; 98 | find_parsed_file(Module, [ParsedFile | Rest]) -> 99 | case get_module(ParsedFile) of 100 | M when M =:= Module -> 101 | ParsedFile; 102 | _ -> 103 | find_parsed_file(Module, Rest) 104 | end. 105 | %% 106 | %% Helpers 107 | %% 108 | 109 | test_beam_path(Module) -> 110 | ?TEST_BEAM_DIR ++ atom_to_list(Module) ++ ".beam". 111 | 112 | dest_dir(AVMFile) -> 113 | ?BUILD_DIR ++ AVMFile. 114 | 115 | get_module(ParsedFile) -> 116 | packbeam_api:get_element_module(ParsedFile). 117 | 118 | get_module_name(ParsedFile) -> 119 | packbeam_api:get_element_name(ParsedFile). 120 | 121 | is_start(ParsedFile) -> 122 | packbeam_api:is_entrypoint(ParsedFile). 123 | 124 | is_beam(ParsedFile) -> 125 | packbeam_api:is_beam(ParsedFile). 126 | 127 | %% 128 | %% Generators 129 | %% 130 | 131 | module_paths() -> 132 | ?LET( 133 | Modules, 134 | list(oneof([a, b, c, d, e, f])), 135 | remove_duplicates([{Module, test_beam_path(Module)} || Module <- Modules]) 136 | ). 137 | 138 | remove_duplicates([]) -> 139 | []; 140 | remove_duplicates([H | T]) -> 141 | [H | [X || X <- remove_duplicates(T), X /= H]]. 142 | -------------------------------------------------------------------------------- /test/test_packbeam.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% Licensed under the Apache License, Version 2.0 (the "License"); 6 | %% you may not use this file except in compliance with the License. 7 | %% You may obtain a copy of the License at 8 | %% 9 | %% http://www.apache.org/licenses/LICENSE-2.0 10 | %% 11 | %% Unless required by applicable law or agreed to in writing, software 12 | %% distributed under the License is distributed on an "AS IS" BASIS, 13 | %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | %% See the License for the specific language governing permissions and 15 | %% limitations under the License. 16 | %% 17 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 18 | 19 | -module(test_packbeam). 20 | 21 | -include_lib("proper/include/proper.hrl"). 22 | -include_lib("eunit/include/eunit.hrl"). 23 | 24 | -define(BUILD_DIR, "_build/"). 25 | -define(TEST_BEAM_DIR, "_build/test/lib/atomvm_packbeam/test/"). 26 | 27 | packbeam_create_simple_test() -> 28 | AVMFile = dest_dir("packbeam_create_simple_test.avm"), 29 | ?assertMatch( 30 | ok, 31 | packbeam_api:create( 32 | AVMFile, [ 33 | test_beam_path("a.beam"), 34 | test_beam_path("b.beam"), 35 | test_beam_path("c.beam"), 36 | test_beam_path("d.beam"), 37 | "test/priv/test.txt" 38 | ] 39 | ) 40 | ), 41 | 42 | ParsedFiles = packbeam_api:list(AVMFile), 43 | 44 | ?assert(is_list(ParsedFiles)), 45 | ?assertEqual(length(ParsedFiles), 5), 46 | 47 | [AFile, BFile, CFile, DFile, TextFile] = ParsedFiles, 48 | 49 | % io:format(user, "~p~n", [ParsedFiles]), 50 | 51 | ?assertMatch(a, get_module(AFile)), 52 | ?assertMatch("a.beam", get_module_name(AFile)), 53 | ?assert(is_beam(AFile)), 54 | ?assert(is_start(AFile)), 55 | ?assert(lists:member({start, 0}, get_exports(AFile))), 56 | 57 | ?assertMatch(b, get_module(BFile)), 58 | ?assertMatch("b.beam", get_module_name(BFile)), 59 | ?assert(is_beam(BFile)), 60 | ?assert(is_start(BFile)), 61 | ?assert(lists:member({start, 0}, get_exports(BFile))), 62 | 63 | ?assertMatch(c, get_module(CFile)), 64 | ?assertMatch("c.beam", get_module_name(CFile)), 65 | ?assert(is_beam(CFile)), 66 | ?assertNot(is_start(CFile)), 67 | ?assertNot(lists:member({start, 0}, get_exports(CFile))), 68 | 69 | ?assertMatch(d, get_module(DFile)), 70 | ?assertMatch("d.beam", get_module_name(DFile)), 71 | ?assert(is_beam(DFile)), 72 | ?assertNot(is_start(DFile)), 73 | ?assertNot(lists:member({start, 0}, get_exports(DFile))), 74 | 75 | ?assertNot(is_beam(TextFile)), 76 | ?assertMatch("test/priv/test.txt", get_module_name(TextFile)), 77 | 78 | ok. 79 | 80 | packbeam_create_start_test() -> 81 | AVMFile = dest_dir("packbeam_create_start_test.avm"), 82 | ?assertMatch( 83 | ok, 84 | packbeam_api:create( 85 | AVMFile, 86 | [ 87 | test_beam_path("a.beam"), 88 | test_beam_path("b.beam"), 89 | test_beam_path("c.beam"), 90 | test_beam_path("d.beam"), 91 | "test/priv/test.txt" 92 | ], 93 | false, 94 | b 95 | ) 96 | ), 97 | 98 | ParsedFiles = packbeam_api:list(AVMFile), 99 | 100 | ?assert(is_list(ParsedFiles)), 101 | ?assertEqual(length(ParsedFiles), 5), 102 | 103 | [BFile | _] = ParsedFiles, 104 | 105 | % io:format(user, "~p~n", [ParsedFiles]), 106 | 107 | ?assertMatch(b, get_module(BFile)), 108 | ?assertMatch("b.beam", get_module_name(BFile)), 109 | ?assert(is_beam(BFile)), 110 | ?assert(is_start(BFile)), 111 | ?assert(lists:member({start, 0}, get_exports(BFile))), 112 | 113 | ok. 114 | 115 | packbeam_create_prune_test() -> 116 | AVMFile = dest_dir("packbeam_create_prune_test.avm"), 117 | ?assertMatch( 118 | ok, 119 | packbeam_api:create( 120 | AVMFile, 121 | [ 122 | test_beam_path("a.beam"), 123 | test_beam_path("b.beam"), 124 | test_beam_path("c.beam"), 125 | test_beam_path("d.beam"), 126 | test_beam_path("e.beam"), 127 | test_beam_path("f.beam"), 128 | "test/priv/test.txt" 129 | ], 130 | true, 131 | b 132 | ) 133 | ), 134 | 135 | ParsedFiles = packbeam_api:list(AVMFile), 136 | 137 | ?assert(is_list(ParsedFiles)), 138 | ?assertEqual(length(ParsedFiles), 5), 139 | 140 | [BFile, CFile, EFile, FFile, TextFile] = ParsedFiles, 141 | 142 | % io:format(user, "~p~n", [ParsedFiles]), 143 | 144 | ?assertMatch(b, get_module(BFile)), 145 | ?assertMatch("b.beam", get_module_name(BFile)), 146 | ?assert(is_beam(BFile)), 147 | ?assert(is_start(BFile)), 148 | ?assert(lists:member({start, 0}, get_exports(BFile))), 149 | 150 | ?assertMatch(c, get_module(CFile)), 151 | ?assertMatch("c.beam", get_module_name(CFile)), 152 | ?assert(is_beam(CFile)), 153 | ?assertNot(is_start(CFile)), 154 | ?assertNot(lists:member({start, 0}, get_exports(CFile))), 155 | 156 | ?assertMatch(e, get_module(EFile)), 157 | ?assertMatch("e.beam", get_module_name(EFile)), 158 | ?assert(is_beam(EFile)), 159 | ?assertNot(is_start(EFile)), 160 | ?assertNot(lists:member({start, 0}, get_exports(EFile))), 161 | 162 | ?assertMatch(f, get_module(FFile)), 163 | ?assertMatch("f.beam", get_module_name(FFile)), 164 | ?assert(is_beam(FFile)), 165 | ?assertNot(is_start(FFile)), 166 | ?assertNot(lists:member({start, 0}, get_exports(FFile))), 167 | 168 | ?assertNot(is_beam(TextFile)), 169 | ?assertMatch("test/priv/test.txt", get_module_name(TextFile)), 170 | 171 | ok. 172 | 173 | packbeam_prune_no_start_module_test() -> 174 | AVMFile = dest_dir("packbeam_prune_no_start_module_test.avm"), 175 | ?assertException( 176 | throw, 177 | _Term, 178 | packbeam_api:create( 179 | AVMFile, 180 | [ 181 | test_beam_path("d.beam"), 182 | test_beam_path("e.beam"), 183 | test_beam_path("f.beam"), 184 | "test/priv/test.txt" 185 | ], 186 | true, 187 | b 188 | ) 189 | ), 190 | 191 | ok. 192 | 193 | packbeam_dest_fail_test() -> 194 | ?assertMatch( 195 | {error, _}, 196 | packbeam_api:create( 197 | "/tmp", [ 198 | test_beam_path("a.beam"), 199 | test_beam_path("b.beam"), 200 | test_beam_path("c.beam"), 201 | test_beam_path("d.beam"), 202 | test_beam_path("e.beam"), 203 | "test/priv/test.txt" 204 | ] 205 | ) 206 | ), 207 | 208 | ?assertMatch( 209 | {error, _}, 210 | packbeam_api:create( 211 | "/usr/should_not_allowed.avm", [ 212 | test_beam_path("a.beam"), 213 | test_beam_path("b.beam"), 214 | test_beam_path("c.beam"), 215 | test_beam_path("d.beam"), 216 | test_beam_path("e.beam"), 217 | "test/priv/test.txt" 218 | ] 219 | ) 220 | ), 221 | 222 | ?assertMatch( 223 | {error, _}, 224 | packbeam_api:create( 225 | "/likely_doesnt_exist/should_fail.avm", [ 226 | test_beam_path("a.beam"), 227 | test_beam_path("b.beam"), 228 | test_beam_path("c.beam"), 229 | test_beam_path("d.beam"), 230 | test_beam_path("e.beam"), 231 | "test/priv/test.txt" 232 | ] 233 | ) 234 | ), 235 | 236 | ok. 237 | 238 | packbeam_src_fail_test() -> 239 | ?assertException( 240 | throw, 241 | _Term, 242 | packbeam_api:create( 243 | dest_dir("packbeam_src_fail_test.avm"), [ 244 | test_beam_path("a.beam"), 245 | test_beam_path("b.beam"), 246 | test_beam_path("c.beam"), 247 | test_beam_path("d.beam"), 248 | test_beam_path("does_not_exist.beam"), 249 | "test/priv/test.txt" 250 | ] 251 | ) 252 | ), 253 | 254 | ok. 255 | 256 | packbeam_create_named_module_test() -> 257 | AVMFile = dest_dir("packbeam_list_test.avm"), 258 | ?assertMatch( 259 | ok, 260 | packbeam_api:create( 261 | AVMFile, [ 262 | test_beam_path("a.beam"), 263 | test_beam_path("b.beam"), 264 | test_beam_path("c.beam"), 265 | test_beam_path("d.beam"), 266 | {"test/priv/test.txt", "any/thing/you/want.txt"} 267 | ] 268 | ) 269 | ), 270 | 271 | ParsedFiles = packbeam_api:list(AVMFile), 272 | 273 | ?assert(is_list(ParsedFiles)), 274 | ?assertEqual(length(ParsedFiles), 5), 275 | 276 | [_AFile, _BFile, _CFile, _DFile, TextFile] = ParsedFiles, 277 | 278 | ?assertNot(is_beam(TextFile)), 279 | ?assertMatch("any/thing/you/want.txt", get_module_name(TextFile)), 280 | 281 | ok. 282 | 283 | packbeam_list_test() -> 284 | AVMFile = dest_dir("packbeam_list_test.avm"), 285 | ?assertMatch( 286 | ok, 287 | packbeam_api:create( 288 | AVMFile, 289 | [ 290 | test_beam_path("a.beam"), 291 | test_beam_path("b.beam"), 292 | test_beam_path("c.beam"), 293 | test_beam_path("d.beam"), 294 | "test/priv/test.txt" 295 | ], 296 | true, 297 | b 298 | ) 299 | ), 300 | 301 | packbeam_api:list(AVMFile), 302 | 303 | ?assertException( 304 | throw, 305 | _Term, 306 | packbeam_api:list(test_beam_path("d.beam")) 307 | ), 308 | 309 | ?assertException( 310 | throw, 311 | _Term, 312 | packbeam_api:list("/usr") 313 | ), 314 | 315 | ?assertException( 316 | throw, 317 | _Term, 318 | packbeam_api:list("/should_not_exist.txt") 319 | ), 320 | 321 | ok. 322 | 323 | packbeam_create_dependent_avm_test() -> 324 | AVMFile = dest_dir("packbeam_create_dependent_avm_test.avm"), 325 | ?assertMatch( 326 | ok, 327 | packbeam_api:create( 328 | AVMFile, [ 329 | test_beam_path("a.beam"), 330 | test_beam_path("b.beam"), 331 | test_beam_path("c.beam"), 332 | test_beam_path("d.beam"), 333 | "test/priv/test.txt" 334 | ] 335 | ) 336 | ), 337 | % io:format(user, "ParsedFiles1: ~p~n", [packbeam_api:list(AVMFile)]), 338 | 339 | AVMFile2 = dest_dir("packbeam_create_dependent_avm_test2.avm"), 340 | ?assertMatch( 341 | ok, 342 | packbeam_api:create( 343 | AVMFile2, 344 | [ 345 | test_beam_path("x.beam"), 346 | AVMFile 347 | ], 348 | true, 349 | x 350 | ) 351 | ), 352 | 353 | ParsedFiles = packbeam_api:list(AVMFile2), 354 | 355 | ?assert(is_list(ParsedFiles)), 356 | ?assertEqual(length(ParsedFiles), 5), 357 | 358 | [FirstFile | _Rest] = ParsedFiles, 359 | ?assertEqual(get_module(FirstFile), x), 360 | ?assert(is_beam(FirstFile)), 361 | ?assert(is_start(FirstFile)), 362 | 363 | ?assert(parsed_file_contains_module(x, ParsedFiles)), 364 | ?assert(parsed_file_contains_module(a, ParsedFiles)), 365 | ?assert(parsed_file_contains_module(b, ParsedFiles)), 366 | ?assert(parsed_file_contains_module(c, ParsedFiles)), 367 | 368 | ok. 369 | 370 | packbeam_extract_test() -> 371 | AVMFile = dest_dir("packbeam_extract_test.avm"), 372 | ?assertMatch( 373 | ok, 374 | packbeam_api:create( 375 | AVMFile, [ 376 | test_beam_path("a.beam"), 377 | test_beam_path("b.beam"), 378 | test_beam_path("c.beam"), 379 | test_beam_path("d.beam"), 380 | "test/priv/test.txt" 381 | ] 382 | ) 383 | ), 384 | 385 | ScratchDir = create_scratch_dir("packbeam_extract_test"), 386 | ok = packbeam_api:extract(AVMFile, [], ScratchDir), 387 | 388 | ?assert(file_exists(ScratchDir ++ "/a.beam")), 389 | ?assert(file_exists(ScratchDir ++ "/b.beam")), 390 | ?assert(file_exists(ScratchDir ++ "/c.beam")), 391 | ?assert(file_exists(ScratchDir ++ "/d.beam")), 392 | ?assert(file_exists(ScratchDir ++ "/test/priv/test.txt")), 393 | 394 | ScratchDir2 = create_scratch_dir("packbeam_extract_test2"), 395 | ok = packbeam_api:extract(AVMFile, ["c.beam", "test/priv/test.txt"], ScratchDir2), 396 | 397 | ?assert(not file_exists(ScratchDir2 ++ "/a.beam")), 398 | ?assert(not file_exists(ScratchDir2 ++ "/b.beam")), 399 | ?assert(file_exists(ScratchDir2 ++ "/c.beam")), 400 | ?assert(not file_exists(ScratchDir2 ++ "/d.beam")), 401 | ?assert(file_exists(ScratchDir2 ++ "/test/priv/test.txt")), 402 | 403 | ScratchDir3 = create_scratch_dir("packbeam_extract_test3"), 404 | ok = packbeam_api:extract(AVMFile, ["c.beam", "does-not-exist"], ScratchDir3), 405 | 406 | ?assert(not file_exists(ScratchDir3 ++ "/a.beam")), 407 | ?assert(not file_exists(ScratchDir3 ++ "/b.beam")), 408 | ?assert(file_exists(ScratchDir3 ++ "/c.beam")), 409 | ?assert(not file_exists(ScratchDir3 ++ "/d.beam")), 410 | ?assert(not file_exists(ScratchDir3 ++ "/test/priv/test.txt")), 411 | 412 | ?assertException( 413 | throw, 414 | _Term, 415 | packbeam_api:extract(AVMFile ++ "-garbage", [], ScratchDir3) 416 | ), 417 | 418 | ?assertException( 419 | throw, 420 | _Term, 421 | packbeam_api:extract(AVMFile, [], ?BUILD_DIR ++ "does-not-exist") 422 | ), 423 | 424 | ?assertException( 425 | throw, 426 | _Term, 427 | packbeam_api:extract(AVMFile, [], ?BUILD_DIR ++ "packbeam_create_dependent_avm_test2.avm") 428 | ), 429 | 430 | ok. 431 | 432 | file_exists(Path) -> 433 | filelib:is_file(Path). 434 | 435 | %% 436 | %% helper functions 437 | %% 438 | 439 | dest_dir(AVMFile) -> 440 | ?BUILD_DIR ++ AVMFile. 441 | 442 | test_beam_path(BeamFile) -> 443 | ?TEST_BEAM_DIR ++ BeamFile. 444 | 445 | get_module(ParsedFile) -> 446 | packbeam_api:get_element_module(ParsedFile). 447 | 448 | get_module_name(ParsedFile) -> 449 | packbeam_api:get_element_name(ParsedFile). 450 | 451 | get_exports(ParsedFile) -> 452 | proplists:get_value(exports, proplists:get_value(chunk_refs, ParsedFile)). 453 | 454 | is_start(ParsedFile) -> 455 | packbeam_api:is_entrypoint(ParsedFile). 456 | 457 | is_beam(ParsedFile) -> 458 | packbeam_api:is_beam(ParsedFile). 459 | 460 | parsed_file_contains_module(Module, ParsedFiles) -> 461 | lists:any( 462 | fun(ParsedFile) -> 463 | get_module(ParsedFile) =:= Module 464 | end, 465 | ParsedFiles 466 | ). 467 | 468 | create_scratch_dir(Name) -> 469 | ok = filelib:ensure_dir(?BUILD_DIR ++ "/" ++ Name ++ "/" ++ "dummy"), 470 | ?BUILD_DIR ++ "/" ++ Name. 471 | -------------------------------------------------------------------------------- /test/x.erl: -------------------------------------------------------------------------------- 1 | %% 2 | %% Copyright (c) 2023 dushin.net 3 | %% All rights reserved. 4 | %% 5 | %% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later 6 | 7 | -module(x). 8 | 9 | -export([start/0]). 10 | 11 | start() -> 12 | a:start(). 13 | --------------------------------------------------------------------------------