Scientific Computing

CMake link macOS Framework

macOS Frameworks such as Foundation are linked with CMAKE_LANG_LINK_LIBRARY_USING_FEATURE.

cmake_minimum_required(VERSION 3.24)

project(macosFramework LANGUAGES OBJCXX)

add_executable(main main.mm)

if(CMAKE_OBJCXX_LINK_LIBRARY_USING_FRAMEWORK_SUPPORTED)

target_link_libraries(main PRIVATE "$<LINK_LIBRARY:FRAMEWORK,Foundation>")

endif()

Example main.mm using “Foundation” macOS framework.

#import <Foundation/Foundation.h>
#include <iostream>

int main() {
    @autoreleasepool {
        NSURL *homeURL = [[NSFileManager defaultManager] homeDirectoryForCurrentUser];
        NSString *homeDirectory = [homeURL path];
        std::cout << "User home directory: " << [homeDirectory UTF8String] << "\n";
    }
    return 0;
}
cmake -Bbuild

cmake --build build -v

Shows the link line used.

Obsolete syntax

The obsolete, pre-CMake 3.24 syntax is like:

target_link_libraries(main PRIVATE
"-framework Foundation"
"-framework IOKit"
)

Cray compiler CMake toolchain

Cray PE can select from multiple compilers as backends for better language standard support while maintaining performance of Cray frontend optimizations. Compilers such as Intel oneAPI themselves use GCC as a backend on Linux. Manual configuration may be required as the default system GCC may be too old for the desired language standard features. Or, the libstdc++ can be too old in the system default GCC. These issues are remediated by purposeful specification of the Cray-Intel-GCC toolchain in a CMake toolchain file.

This toolchain file can be copied to a common location like ~ and used among many projects, such as cray.cmake.

For CMake project that have ExternalProject inside them, there must be a CMAKE_TOOLCHAIN_FILE parameter to ExternalProject_Add like:

set(cmake_args ...)

if(CMAKE_TOOLCHAIN_FILE)
  list(APPEND cmake_args -DCMAKE_TOOLCHAIN_FILE:FILEPATH=${CMAKE_TOOLCHAIN_FILE})
endif()


ExternalProject_Add(...
CMAKE_ARGS ${cmake_args}
)

The CMake project is configured like:

cmake --toolchain ~/cray.cmake -B build

Intel oneMKL LAPACK95 with Gfortran

On Linux, the Intel oneMKL LAPACK95 library can also be used with Gfortran. Build and install LAPACK95 with Intel MKL Lapack95 project.

An example CMakeLists.txt, using the FindLAPACK.cmake under cmake/ of Netlib LAPACK95

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

find_package(LAPACK COMPONENTS LAPACK95 REQUIRED)

add_executable(myexe myprogram.f90)
target_link_libraries(myexe PRIVATE LAPACK::LAPACK95)

GDL datalanguage build

GDL (GNU Data Language) is a free/libre open-source program that runs most IDL code. GDL is actively developed and easily installed by:

  • Linux: apt install gnudatalanguage
  • macOS Homebrew: brew install gnudatalanguage
  • Windows: get the latest release

Building GDL source uses the GDL build script “scripts/build_gdl.sh” to get the prerequisites. If Anaconda Python is present, conda deactivate first to avoid library problems when building GDL.

cmake -B build --install-prefix=$HOME/gdl

cmake --build build --parallel

(optional) Check the install. You will see several plots appearing and disappearing automatically during this test, which takes a few minutes.

cmake --test-dir build -V

Install (do not use sudo):

cmake --install build

Do not build on an ExFAT / FAT32 drive, as the build will fail since symbolic links are not allowed on ExFAT / FAT32. If cmake reports libeigen being too old, install LibEigen3 or:

cmake -Bbuild -DEIGEN3=OFF

To use the Linux distro’s older version of GDL, just use /usr/local/bin/gdl or similar, or rename ~/.local/bin/gdl to ~/.local/bin/gdl0.98 or similar.

Troubleshooting build:

  • Runtime search path conflicts: temporarily comment out those paths in ~/.profile (typically from Anaconda Python, libreadline, libhistory, libz, libjpeg.so)
  • Problems with LZMA, try disabling HDF5: cmake -DHDF5=OFF

CMake get CPU count

For moderately advanced tasks in CMake it can be useful to get the CPU count to allow building or testing in parallel with non-CMake-based ExternalProject. Building a non-CMake ExternalProject from CMake with GNU Make by default builds serially, which can be very slow. This is because CMake doesn’t know if the non-CMake ExternalProject is capable of being built in parallel. When it’s known that the non-CMake-based ExternalProject can be built in parallel, it’s necessary to get the CPU count in CMake as simply putting make -j can overwhelm the computer.

Get CPU count in CMake via cmake_host_system_information. cmake_host_system_information should be used instead of ProcessorCount. The CMake module script ProcessorCount is known to be not as accurate as the C++-based cmake_host_system_information.

Example: specify the parameters relevant to CPU count:

cmake_host_system_information(RESULT Ncpu QUERY NUMBER_OF_PHYSICAL_CORES)
message(STATUS "CMake ${CMAKE_VERSION} using ${Ncpu} threads")

include(ExternalProject)

ExternalProject_Add(demo
BUILD_COMMAND make -j${Ncpu}
)

CB Radio CTCSS tone code squelch

Continuously transmitted subaudible squelch systems such as CTCSS (Continuous Tone-Coded Squelch) and DCS (Digital Coded Squelch) are available on FM radios including CB radios so equipped. CTCSS on CB radio allows maximum range while virtually eliminating unwanted static and skip from other users. FCC WT Docket 10-119 permits FM frequency modulation, optionally with CTCSS and DCS on CB radio, along with FM mode and other long-awaited and useful features to the American public. We will call CTCSS and DCS “coded squelch” for short. Subaudible coded squelch is reliable for communications systems that have adequately high SNR and are frequency stable. For example, satellite downlinks, broadcast radio remote control, utility control, broadcast radio identification, which are all applications that have a long time to decode repetitive signals. Mobile two-way radio has typically weak and fading signals with interference from co-channel users and general noise. The squelch should open quickly (say with half a second) and close quickly at the end of the transmission to avoid annoying bursts of noise. The coded squelch must tolerate Doppler shift and frequency drift between transmitters and receivers.

Subaudible continuous coded squelch is not available on AM modes in general. Some communications receivers have coded squelch that also seems to work in AM, whether by accident or design. I have tested such receivers with a signal generator and on AM mode, coded squelch can seem to “work” on the bench. Why then isn’t AM mode CTCSS or DCS generally available?

The following sections explain why CTCSS and DCS are only useful on FM radios. Selective calling on AM and SSB is available by on-channel signaling such as Selcall CCIR 493-4. Selcall CCIR 493-4 is a selective calling system that works on AM and SSB by using in-band signaling that takes several seconds at the beginning of a conversation to open the squelch of one, many, or all receivers in a group. Selcall is used in HF marine radio, HF land mobile radio, and HF amateur radio and is also legal for CB radio in the USA.

Threshold effect and capture effect

FM receivers have a threshold effect that makes them less susceptible to noise than AM receivers. Once the input signal exceeds the noise floor sufficiently, the output SNR increases dramatically. This is the threshold effect. The threshold effect is why FM is less susceptible to noise than AM. The FM capture effect is a similar phenomenon where the FM receiver tends to hear only the strongest signal once it’s a little bit stronger than all the other signals.

These two effects make CTCSS, DCS, and other subaudible modulation like LTR work extremely well on FM, but are completely impractical on AM for mobile two-way communications systems.

SNR compromises

Continuous subaudible coded squelch modulation must not consume too much of the available modulation bandwidth, or the output SNR (audio quality at the receiver’s speaker) will suffer. In FM systems, the deviation of the subaudible information has generally been accepted by industry and de facto standards in the range of 15% .. 35% of the deviation of the main audio signal. For example, on obsolete 5kHz deviation FM systems, the CTCSS, DCS, or trunked subaudible deviation would be 750-1000Hz. In 2.5kHz deviation FM systems, the subaudible deviation would be 375-500Hz for CTCSS or DCS, and 800 Hz for LTR trunked subaudible modulation. Practical experience with 450 MHz and 900 MHz SMR trunked systems led some operators to set the LTR subaudible deviation to 1000 Hz instead of 800 Hz despite the 2.5 kHz maximum total FM deviation, compromising the audio quality and output SNR of the main signal.

If we apply the same principles to AM, we would have to reduce the modulation depth of the main audio signal to accommodate the subaudible tone. This would reduce the output SNR of the main signal, which is already compromised by the nature of AM. The output SNR of AM is generally lower than FM, and the modulation depth of AM is already limited by the need to avoid overmodulation. Adding a subaudible tone to AM would further reduce the modulation depth of the main signal, reducing the output SNR of the main signal. This would reduce or eliminate the slight advantage AM has over FM in weak input SNR conditions.

By way of reference, in broadcast AM (530-1710 kHz) the subaudible modulation depth by 47 CFR § 73.1570 (b)1(ii) is limited to 6% of the total modulation depth. Utilities that used broadcast AM subaudible tones for remote control of transmitters with such little modulation assumed a powerful signal and consistent input SNR with fixed location receivers. Canadian patent CA1149023A discusses using angle modulation of the carrier frequency within 20 Hz of nominal channel center frequency to avoid the typical 5% modulation depth limit of AM. The broadcast AM subaudible signaling standard was field tested in about 1969 as discussed in FCC Docket 17873 that permitted AM broadcast subaudible signaling. The C-QUAM AM stereo system uses a 25 Hz phase-modulated tone at 5% deviation to indicate stereo availability. Once again, strong signals and consistent input SNR are necessary for successful detection of the pilot tone, and detection is not near-instantaneous as required for coded squelch.

Suppose a 5% subaudible modulation depth is used on CB Radio for coded squelch. The power in each sideband of the subaudible tone would be 50 milliwatts (0.05 Watts) for a 4 watt carrier. The input SNR would have to be sufficient to decode the subaudible tone. The output SNR of the main signal would be reduced by the subaudible tone.

Thus the useful communications range between stations would be so dramatically reduced to open the coded squelch that the feature would be impractical with AM mode.

Beat frequencies false squelch

When more than one carrier is above the noise level in an AM communications receiver, the carriers produce a beat frequency defined by the difference in carriers’ frequencies. The beat frequencies may typically fall within the subaudible tone range given modern transmitter frequency stability. This can cause false squelch opening on the receiver. Conversely, the beat frequencies may interfere with the subaudible tone, causing the receiver to not open squelch when it should.

CTest CDash run only once

This CMake function works across Windows, macOS, and Linux checks that the current CTest script is the only instance of CTest running at the time. We use this in CTest CDash scripts to avoid unexpectedly long-running scripts overlapping with the next day’s runs. That could exhaust memory and crash the computer.

Git commit PGP signing

Git signed commits help verify the Git author’s identity using PGP. Optionally, a user or organization can set rules requiring Git PGP signed commits on Git hosting providers such as GitHub and GitLab

PGP public keys can help verify author identity of Git commits, social media, website, etc. Setup GPG on the laptop:

  • Linux: apt install gnupg
  • macOS: brew install gnupg
  • Windows: winget install gnupg.Gpg4win

Generate a GPG keypair if one doesn’t already exist for yourself.

Export the GPG public and private key and import into GPG:

gpg --full-generate-key
gpg --armor --export-secret-keys > private_key.asc
gpg --armor --export > public_key.asc

Verify PGP key:

gpg --list-secret-keys --keyid-format LONG

The first lines will be like:

sec   rsa4096/<public_hex>

The hexadecimal part after the / is a public reference to the GPG keypair.


Add Git provider such as GitHub or GitLab verified email address to the PGP key. To make commits “Verified” with the Git provider, at least one of the Git provider verified email addresses must match:

git config --get user.email

Use the GPG public ID below:

gpg --edit-key <public_hex>

In the interactive GPG session that launches, type

adduid

and enter Name and the Email address–which must exactly match the GitHub verified email address. I also add the @users.noreply.github.com fake email that I always use to avoid spam. Do adduid twice–once for the real GitHub verified email address and again for the github_username@users.noreply.github.com fake email.

Add “trust” from the GPG> prompt:

trust

Since it’s you, perhaps a trust level of 5 is appropriate. type

save

to save changes, which may not show up until exiting and reentering the GPG> prompt.

Configure Git to use the GPG public hex ID like:

git config --global user.signingkey <public_hex>

git config --global commit.gpgsign true

Add the GPG public key to the Git provider. Copy and paste the output from this command into GPG Key of GitHub or GitLab. This is done only once per human, not once per device.

gpg --armor --export <public_hex>

Windows

On Windows, additionally do

git config --global gpg.program "$Env:ProgramFiles (x86)\GnuPG\bin\gpg.exe"

macOS

On macOS, additionally do

brew install pinentry-mac

and add to ~/.zshrc:

export GPG_TTY=$TTY

Usage

GPG can be used to sign Git commits and tags, and can also be disabled per commit.

Verify Git PGP commit sign

Make a git commit after the procedure above, and see the signature notes:

git log --show-signature

it will start with

gpg: Signature made

Temporary disable Git commit sign

If you temporarily lose access to the GPG password, you won’t be able to git commit. A temporary workaround is to set

git config commit.gpgsign false

or simply add the --no-gpg-sign option like:

git commit -am "msg" --no-gpg-sign

If not signing as default, sign only certain commits by

git commit -S

Note that’s a capital “S”.

Keybase.io PGP key export

Public PGP IDs can help verify author identity of Git commits, social media, website, etc. A popular free service to share PGP IDs is Keybase.io. Below we demonstrate using Keybase.io PGP ID with Git with the keybase.io client.

Setup GPG on the laptop:

  • Linux: apt install gnupg
  • macOS: brew install gnupg
  • Windows: winget install gnupg.Gpg4win Keybase.Keybase

Export Keybase.io public & private key and import into GPG:

keybase pgp export | gpg --import

keybase pgp export --secret | gpg --pinentry-mode=loopback --allow-secret-key --import

Verify PGP key:

gpg --list-secret-keys --keyid-format LONG

The first lines will be like:

sec   rsa4096/<public_hex>

The hexadecimal part after the / is a public reference to keybase.io keypair. It’s shown on the keybase.io public profile, next to the key icon.