Modules and packages
How to use someone else's library and how to release your own. flang has no package manager; there are two ways:
| Way | What it is | How it is imported |
|---|---|---|
| a package | one file with a library and everything it imports | the file is next to the program: использует «X» из "x.flang-package" |
| a module by link | one .flang file at an address | использует «X» из "https://…"; the compiler downloads it once and records it in flang.lock |
Two commands are needed: flang package builds a package, flang lock prints the lock file of a whole program. Their flags are in the Command reference. The examples below use the directory docs/examples/package/ from the repository.
Use a package
- Put the package file next to your program.
- Import it by its module name:
модуль «Shop»
использует «Скидка» из "discount.flang-package"
- Check and run the examples as usual:
$ ls
discount.flang-package shop.flang
$ flang check shop.flang
модуль «Shop»: функций 4, из них с доказанным завершением 4; типов 0; файлов вместе с импортами 2
shop.flang: проверено — доказано 0, проверено только на тестах 0, не доказано 0 (постусловий и утверждений 0); замечаний нет
$ flang test shop.flang
shop.flang: примеров 7, прошло 7, не прошло 0
файлов вместе с импортами 2 (2 files with imports) — the program and the module from the package. The directory has no library sources: its functions, types and examples are in the package file.
Two errors that happen when a package is imported:
| Error | When | What to do |
|---|---|---|
FLANG_IMPORT_NAME | the name in the import does not match the module name inside the package; the message gives both names | fix the name in the import |
FLANG_PRECONDITION_CALL | your call does not meet a precondition (требует) of a library function | meet the precondition: check the argument or declare the same требует on your function |
The preconditions of library functions come with the package. Remove требует from your own function «Сколько скинули», and the program stops passing the check, though the library did not change:
$ flang check shop.flang
…
FLANG_PRECONDITION_CALL, строка 20, столбец 3: вызов «Скидка в копейках» в функции «Сколько скинули» не снимает предусловие «доля не больше ста»: …
shop.flang: не проверено — замечаний 1
The message says: the call on line 20 does not meet the precondition «доля не больше ста» (the share is at most one hundred).
Import a module by a link
The address of the module is written right in the import line:
модуль «Магазин»
использует «Скидка» из "https://example.org/discount/v1.2.0/discount.flang"
$ flang check shop.flang
скачано: https://example.org/discount/v1.2.0/discount.flang — отпечаток 9261f268…
в замок /home/me/shop/flang.lock: ссылок 1
модуль «Магазин»: функций 4, из них с доказанным завершением 4; типов 0; файлов вместе с импортами 2
shop.flang: проверено — доказано 0, проверено только на тестах 0, не доказано 0 (постусловий и утверждений 0); замечаний нет
The first line gives the address and the fingerprint (sha256) of the downloaded file. The second says it was recorded in flang.lock. On later runs these two lines are absent: the file is taken from flang.lock.
The rules:
| Question | Answer |
|---|---|
| what counts as a link | a string that starts with https:// or file:// |
| where the module text is looked for | first in flang.lock next to the program, then in the cache $XDG_CACHE_HOME/flang/links/ (without the variable, ~/.cache/flang/links/), then on the network |
| what is recorded | the fingerprint and the text itself — in flang.lock; the text — in the cache as well |
| imports of the downloaded module | resolved the same way; a relative path in it is counted from its address |
| what is checked | the downloaded module is checked together with the program, like your own file: types, termination, postconditions and examples |
| versions | there is no version selection. A link names one file; a new version is a new link (/v1.2.0/ → /v1.3.0/) |
| if the file at the address is later replaced | the build does not notice: it takes the text from flang.lock and compares it with the fingerprint |
| without a network | with FLANG_OFFLINE=1 the text is taken only from flang.lock and the cache |
Building on another machine needs the program and its flang.lock. Such a build does not need the network.
Link errors
The exit code of all of them is 1. The message holds the error code, the link and the reason.
| Code | What it means | What to do |
|---|---|---|
FLANG_LINK_FETCH | the file was not received: the address did not answer 200, there is no file, the scheme is neither https:// nor file:// | check the link |
FLANG_LINK_OFFLINE | FLANG_OFFLINE is set, and neither flang.lock nor the cache has this link | build once with a network or bring flang.lock |
FLANG_LINK_CHANGED | the text in flang.lock or in the cache does not match its fingerprint | restore flang.lock from git; to download again, remove the entry from flang.lock and clear the cache |
Make a package
The library needs nothing special: it is an ordinary module with the header модуль / экспортирует. Below it is in skidka/discount.flang.
Step 1. Write flang.package
Put the file flang.package next to the entry file of the library:
{
"имя": "Скидка",
"версия": "1.0.0",
"источник": "https://github.com/digitable-lol/flang"
}
| Field | Required | What it means |
|---|---|---|
имя (name) | yes | the package name; it must match the module name in the first line of the entry file — the package is imported by it |
версия (version) | yes | the package version |
источник (origin) | no | the address of the sources |
If the names differ, flang package ends with an error:
$ flang package skidka/discount.flang
FLANG_PACKAGE: в flang.package пакет назван «Не Скидка», а модуль в skidka/discount.flang называется «Скидка»
$ echo $?
1
In the example from the repository the names differ too: the module in docs/examples/package/discount.flang is called «Discount», and the flang.package next to it says «Скидка». flang package answers it with the same error. The ready package shop/discount.flang-package was built from a module named «Скидка».
Step 2. Build the package
$ flang package skidka/discount.flang > skidka/discount.flang-package
$ ls skidka/
discount.flang discount.flang-package flang.package
First flang package makes the same checks as flang check. No package is built from a program with an error.
A package is a JSON file. This is how flang package … --pretty prints it; the source is shortened:
{
"схема": 2,
"имя": "Скидка",
"версия": "1.0.0",
"вход": "./discount.flang",
"модули": [
{
"имя": "Скидка",
"путь": "./discount.flang",
"функций": 2,
"адрес": "0a6b34a007373520949fd5b5a83f748016ad6a2469cf1ec09366112d2e47c6b9",
"исходник": "модуль «Скидка»\n экспортирует «Скидка в копейках», «Цена за вычетом»\n…"
}
],
"ведомость": [
{
"функция": "Цена за вычетом",
"утверждение": "цена за вычетом не выходит за точный потолок",
"сила": "доказано"
}
],
"источник": "https://github.com/digitable-lol/flang",
"печать": "9154d2f59a11820e976336501c3b8ce593bc589c4e2e8829e8dc745b6f9559ea"
}
| Field | What it holds |
|---|---|
модули (modules) | every module of the library: name, path, number of functions, address and the full source — not compressed, not base64 |
адрес (address) | the sha256 of the module source, 64 hexadecimal characters. When the package is read, the source is compared with the address, so a change of even one byte is detected |
ведомость | the proof report: for each postcondition of the library, whether it is proved, only tested or not proved |
печать | a checksum over the name, the version, the origin and the function count of every module |
The proof report helps to choose a library. You do not have to rely on it: the package holds the full source, and on import your compiler proves everything again.
Step 3. Publish
Commit one file:
$ git add skidka/discount.flang-package
$ git commit -m "Скидка 1.0.0"
$ git push
A user takes this file any way they like: by a direct link, from a release, by mail. There is no flang publish command.
A library of several modules
flang package follows the imports from the entry file, and every module of the library goes into the package. The user of the package does not see this.
The multi-module example docs/examples/library-api/lib/ has no flang.package, so flang package on its api.flang answers FLANG_PACKAGE: рядом с … нет объявления flang.package. Put a flang.package next to api.flang, and the steps are the same as above.
A package may import another package:
verh.flang использует «Скидка» из "discount.flang-package"
verh.flang-package holds both «Верх» and «Скидка»
Whoever imports verh.flang-package does not need the file discount.flang-package: it is inside.
Change the version
The version is written in flang.package. To release a new one, edit it and build the package again:
$ sed -i 's/"версия": "1.0.0"/"версия": "1.1.0"/' skidka/flang.package
$ flang package skidka/discount.flang --pretty | grep '"версия"'
"версия": "1.1.0",
Do not edit the version inside a built package: the checksum and the module addresses are recomputed on reading, and an edited package is not accepted.
$ sed -i 's/"версия":"1.0.0"/"версия":"9.9.9"/' vitrina/discount.flang-package
$ flang check vitrina/shop.flang
FLANG_PACKAGE: печать пакета «Скидка» не сходится: пакет правлен или испорчен
$ echo $?
1
There are no version ranges (^1.2, ~> 1.2). A program uses the file that is next to it, and that file does not update itself.
Build offline
A package does not need the network: the code is already in the file. A module by link is downloaded once; after that it is taken from flang.lock.
To make sure a package gives the same result as the library sources, generate the program both ways and compare the directories:
flang emit shop.flang --target c --out ./from-package # in the directory with the package
flang emit shop.flang --target c --out ./from-sources # in the directory with the sources
diff -r ./from-package ./from-sources && echo same
If a package was edited after it was built
FLANG_PACKAGE message | What was changed |
|---|---|
адрес модуля «Скидка» не сходится с его исходником: правлен или испорчен | the source of a module, at least one character |
печать пакета «Скидка» не сходится: пакет правлен или испорчен | the version, the package name, the origin or the function count of a module |
путь …/obshee.flang привезли два пакета с разным содержимым: … | nothing; two packages hold the same module path with different text. The message names both packages. Bring both libraries to one version |
печать in the message is the checksum of the package. It shows that the file was not edited after it was built. It is not a signature: it does not say who built the file.
If two packages hold one path with the same text, the program compiles: packages are compared by content, not by file name.
A lock file versus a package
Both keep code in a file, so they are easy to confuse.
flang lock | flang package | |
|---|---|---|
| answers the question | "what is this program built from" | "here is a library, take it" |
| name and version | none | required |
| how it is used | lies next to the program as flang.lock | imported with использует … из "…" |
| how many per program | one | any number |
| the checksum covers | the modules | the modules, the name, the version, the origin, the function count |
A program may have both a flang.lock and packages.
What is missing
| What is missing | What there is instead |
|---|---|
the commands flang install, flang search, flang publish | a module is imported by a link; publishing is a commit of the file at a known address |
| a package registry | a module is named by its address |
| two versions of one library in one program | imports merge into one shared namespace |
| partial updates | run flang package again |
| a signature of the author | the checksum only shows that the file was not edited |
packages in flang repl | the shell does not read packages: flang repl shop.flang prints FLANG_PARSE, заголовок модуля, строка 1 and does not see the library's functions |
packages in flang lsp | not tested |
Next
- Command reference — the flags of
flang packageandflang lock. - Embedding flang — how a library becomes code in your language.
- Roadmap — when the missing pieces are planned.