<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sayantan Ghosh on Medium]]></title>
        <description><![CDATA[Stories by Sayantan Ghosh on Medium]]></description>
        <link>https://medium.com/@sayantanghosh_38070?source=rss-b977d596d5b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*mIsJI1Q9KFIOUGXp.</url>
            <title>Stories by Sayantan Ghosh on Medium</title>
            <link>https://medium.com/@sayantanghosh_38070?source=rss-b977d596d5b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:32:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sayantanghosh_38070/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Installing high energy astrophysics software on linux]]></title>
            <link>https://medium.com/@sayantanghosh_38070/installing-high-energy-astrophysics-software-on-linux-2ec54207b832?source=rss-b977d596d5b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ec54207b832</guid>
            <category><![CDATA[computing-software]]></category>
            <category><![CDATA[guides-and-tutorials]]></category>
            <category><![CDATA[high-energy-astrophysics]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Sayantan Ghosh]]></dc:creator>
            <pubDate>Sat, 14 Feb 2026 17:20:22 GMT</pubDate>
            <atom:updated>2026-02-14T17:20:22.967Z</atom:updated>
            <content:encoded><![CDATA[<p>I had drafted this in 2023 as a general tutorial for graduate students and researchers working in high energy astrophysics data analysis, but I never published it. I suppose I waited to publish it before I finished writing the MacOS part. Hope it still helps.</p><p>High energy astrophysics software that I use routinely for my work has a pretty long history of independent package development. Three main software products that we use are <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/">HEASoft</a> (by the NASA GSFC), <a href="https://cxc.cfa.harvard.edu/ciao/">CIAO</a> (by the Chandra X-ray Observatory) and <a href="https://www.cosmos.esa.int/web/xmm-newton/sas-news">SAS</a> (by the European Space Agency). These softwares while independently developed for data reduction and processing for the corresponding telescopes, require quite a few tools from each other. And being independent development branches, have well documented clashes. So here is my installation scenario, where we use <a href="https://cxc.cfa.harvard.edu/ciao/">CIAO</a> and <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/">HEASoft</a> (for data reduction), with Sherpa and XSpec (for spectral analysis). I also use a few other packages on top of this base, which require sherpa to be compiled with xspec support.</p><h3>Installation on Linux</h3><p>We are going to install on Ubuntu 20.04, so this is going to be a fun challenge. Specifically because the gcc, g++, and gfortran are version 9.</p><pre>conda create -n heastro python=3.10 &quot;numpy&lt;1.24&quot; &quot;setuptools&lt;60&quot; scipy<br>conda activate heastro</pre><p>Also, there are some issues with the xspec being configured only with numpy&lt;1.24and setuptools&lt;60. And installing scipylater seems to update the numpy version causing a rather <em>unpleasant</em> situation. Now create a new directory where all the HEASTRO software will be downloaded and installed. In my case I installed it at /disk2/user/heastro_software. <br>Let’s create a path link <br>export HEASTRO_PATH=/disk2/user/heastro_software. This is to make our life easier while installing various parts of the software.</p><h3><strong><em>Installing HEASoft</em></strong></h3><p>Get the heasoft-6.31.1 from the HEASARC downloads <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/download.html">page</a>. Select the “<a href="https://heasarc.gsfc.nasa.gov/FTP/software/lheasoft/lheasoft6.31/heasoft-6.31.1src.tar.gz">complete download HEASoft source code (all mission and general use software)</a>” option. Note that the above link might change for a future release or patch. It can be downloaded directly via</p><pre>wget -c https://heasarc.gsfc.nasa.gov/FTP/software/lheasoft/lheasoft6.31/heasoft-6.31.1src.tar.gz</pre><p>Grab a coffee while it downloads, and read the installation <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/install.html">documentation</a>. Pay special attention to the caveats since we are going to install multiple software with potential clashes, we will follow only until step 7 in the <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/install.html">documentation</a>. Also, let’s set up our compiler flags in the meantime.</p><pre>CC=/usr/bin/gcc-9<br>CXX=/usr/bin/g++-9<br>FC=/usr/bin/gfortran-9<br>PERL=/usr/bin/perl<br>PYTHON=/home/user/anaconda3/envs/heastro/bin/python3<br>unset CFLAGS CXXFLAGS FFLAGS LDFLAGS</pre><p>Hopefully your linux version will be different (read more recent), so the compilers above must be carefully setup to reflect the latest versions, for the gnu compilers they should be at least version 12. You can check the version of your different compilers using --version and which. For example, you can check the gcc version by issuing gcc --version and the path for the compiler which gcc. Also remember to unset the compiler flags as above. Since we are interested in installing HEASoft in a conda environment, we have specified the PYTHON as above. Also be sure to check that your perl version is at least 5.30, suitably 5.34 at the time of writing.</p><p>Now let’s install heasoft-6.31.1. Since I want to keep all my software in one place, I have set up the $HEASTRO_PATH at the very beginning. Assuming that the heasoft-6.31.1src.tar.gz was downloaded in your /home/user/Downloads, we can extract this tar file at $HEASTRO_PATH by</p><pre>tar -zxvf /home/user/Downloads/heasoft-6.31.1src.tar.gz --directory $HEASTRO_PATH</pre><p>Remember that we don’t particularly the $HEASTRO_PATH variable. I just use it for convenience. It will get removed when the terminal is closed/restarted, so no harm is done at this point. Now we follow the standard instructions from the <a href="https://heasarc.gsfc.nasa.gov/docs/software/lheasoft/install.html">installation guide</a> to configure and install heasoft-6.31.1. Take note of where you want to install your heasoft-6.31.1. This has to specified at configure time.</p><pre>cd $HEASTRO_PATH/heasoft-6.31.1/BUILD_DIR<br>./configure --prefix=$HEASTRO_PATH/heasoft<br>make &gt; build.log 2&gt;&amp;1 &amp;<br>make install &gt; install.log 2&gt;&amp;1 &amp;</pre><p>These commands configure, build and install heasoft-6.31.1 to $HEASTRO_PATH/heasoft in the background. You can check the real time progress of your installation by issuing tail -f config.log, tail -f build.log, tail -f install.log which are automatically created in $HEASTRO_PATH/heasoft-6.31.1/BUILD_DIR. Note that once each of the configure, make, and make install steps are done, you can (and most definitely should) check the log files for errors and warnings. This can be done by using for example less config.log | grep &#39;\*\*\*&#39;, since the errors and warnings all start with *** in the log files. If all looks okay, exit the BUILD_DIRand cd $HEASTRO_PATH/heasoft. Your newly installed heasoft-6.31.1 is now installed at $HEASTRO_PATH/heasoft/x86_64-pc-linux-gnu-libcx.yy. In my case x.yy=2.31, but it will most certainly be different in your case.</p><p>Now comes the important part of protecting our heasoft installation and initialising it such that conflicts with the other software that we will install are minimised, while still being able to access the packages in heasoft. For this, we use the hwrap utility wrapper provided by the good folk at HEASARC. Download the hwrap utility into $HEASTRO_PATH from the <a href="https://heasarc.gsfc.nasa.gov/lheasoft/hwrap.html">HEASARC website</a>. Also read the documentation there, since it gives you an idea of the potential conflicts to keep in mind as we go along building and testing heastro.</p><pre>wget -c https://heasarc.gsfc.nasa.gov/FTP/software/lheasoft/release/heasoft-6.31/BUILD_DIR/hwrap<br>mv hwrap hwrap.sh<br>chmod u+x hwrap</pre><p>Now we can run hwrap to wrap the functions and pacakges in $HEASTRO_PATH/heasoft/x86_64-pc-linux-gnu-libc2.31.</p><pre><br>bash hwrap.sh $HEASTRO_PATH/heasoft/x86_64-pc-linux-gnu-libcx.yy $HEASTRO_PATH</pre><p>This will create the links to the heasoft packages in $HEASTRO_PATH/heasoft_wrap/. Add the following lines to your /home/user/.bashrc:</p><pre># HEASOFT using HEASOFT_WRAP<br>export PATH=&quot;$HEASTRO_PATH/heasoft_wrap:$PATH&quot;</pre><p>Remember to use the full path for $HEASTRO_PATH, since we dont want $HEASTRO_PATH to linger in our $PATH. Now restart your terminal in which case you will have to remember to reactivate your conda environment, or simply issuesource ~/.bashrc. I recommend the first option, since it gives you a way declutter the terminal and start afresh for the next steps. Your installation should now be ready to be used.</p><p>Let’s check the installation is really active. Start with the basic fversion, this should give you</p><pre>(base) user@host:~$ fversion<br>16Dec2022_V6.31.1<br>(base) user@host:~$ </pre><p>Notice how we haven’t yet activated our heastro conda environment but fversion still can be accessed. This is because we exported the heasoft_wrap into the $PATH which got initialised at start of the session. There is nothing to worry about here. With the activation of the heastro environment also, we will be able to see the same thing. If you haven’t activated heastro environment after restarting the terminal, please do that using conda activate heastro. Let’s check that xspec is loading its own ipython environment. Issue xspec. This should give you</p><pre>(heastro) user@host:~$ xspec<br><br>  XSPEC version: 12.13.0c<br> Build Date/Time: Tue May 30 08:07:50 2023<br><br>XSPEC12&gt;</pre><p>You can now exit xspec by issuing exit at the XSPEC12&gt; prompt. The installation documentation for heasoft generally ends here since it would be a little too long for mission and instrument specific instructions.</p><p>Although this is not HEASoft tutorial, I like to test that all the things that I need from this package are working correctly at this point. So let’s take a little detour (hopefully an excited break from software installation) and do some NuStar data processing. I happen to use NuStar and Chandra data a lot, so I will only talk about these missions here.</p><p>Let’s take as an example the Centaurus A located at (13 25 15.9, –43 01 04) in the ICRS frame, shown below in the DSS2 red band to show you how pretty it is.</p><figure><img alt="From https://skyview.gsfc.nasa.gov/tempspace/fits/skv39677707352798_9.jpg" src="https://cdn-images-1.medium.com/max/1024/1*oyAL0xatcqctG3j33GAc1w.jpeg" /><figcaption>Centaurus A in the DSS2 Red band.</figcaption></figure><p>Searching for this on the <a href="https://heasarc.gsfc.nasa.gov/cgi-bin/W3Browse/w3browse.pl">HEASARC archive</a> for NuStar, we select the observation ID (obsID) 60001081002 which was observed at 2013–08–06 13:01:07 as a part of the Active Galaxies and Quasars science program. The osbervation details are</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qOTNAjVspZnYigw0i4BKKw.png" /><figcaption>Observation information for the test data of Centaurus A from NuStar.</figcaption></figure><p>The data file is approximately ~1Gb, and can be downloaded following the instructions on the search results page. Extract the downloaded tar archive to a data location of your choice. In my case, I extracted it to</p><pre>tar -xvf ~/Downloads/w3browse-161511.tar --directory=/home/user/data/CenA/</pre><p>Note that you will have a different set of numbers as the downloaded tar archive’s name. However, upon extraction, you will get a directory with the obsID as its name. In this case it will be/home/user/data/CenA/60001081002/. Check the directory contents, you should get event files after full (stages 1–3) processed event_cl output files, level 1 processed event files:event_uf, and auxiliary:auxil, housekeeping:hk, catalogue of files in the archive: cat.gz, and nupipelinelog files.</p><pre>(heastro) user@host:/home/user/data/CenA/60001081002$ ls<br>auxil  event_cl  event_uf  hk  nu60001081002.cat.gz  pipe.log<br>(heastro) user@host:/home/user/data/CenA/60001081002$ </pre><p>Now that we the observation files, time to put heasoft to work. Almost immediately we run into a problem, we haven’t set up heasoft to work with NuStar data yet. We need to install the calibration database CALDB, before we can use heasoft to to anything with NuStar data. In fact, this is something we will encounter every time we work with a new mission or instrument. So let’s get on it.</p><p>The HEASARC <a href="https://heasarc.gsfc.nasa.gov/docs/heasarc/caldb/caldb_supported_missions.html">calibration database</a> can be installed using the given <a href="https://heasarc.gsfc.nasa.gov/docs/heasarc/caldb/caldb_install.html">instructions</a>. Download the NuStar <a href="https://heasarc.gsfc.nasa.gov/FTP/caldb/data/nustar/fpm/goodfiles_nustar_fpm_20230516.tar.gz">CALDB</a> (~1.5Gb) and <a href="https://heasarc.gsfc.nasa.gov/FTP/caldb/data/nustar/fpm/goodfiles_nustar_fpm_clockcor_20230516.tar.gz">patch</a> (~474Mb) files. Note that these links may not be current and you should check the release date before downloading. Also, browsing through the <a href="https://heasarc.gsfc.nasa.gov/docs/heasarc/caldb/caldb_supported_missions.html">calibration database</a> page, we notice that the calibration files are listed for various missions, including the Chandra. Since we will be installing CIAO soon, we will try to setup our local calibration database $CALDB such that we have the calibration files set up for both the missions. The latest release date of the NuStar caldb20230516 was May 16, 2023, and the CIAO caldb-4.10.4, is April 5, 2023. Thus, the following files are to be downloaded</p><pre># NuStar<br>wget -c https://heasarc.gsfc.nasa.gov/FTP/caldb/data/nustar/fpm/goodfiles_nustar_fpm_20230516.tar.gz<br>wget -c https://heasarc.gsfc.nasa.gov/FTP/caldb/data/nustar/fpm/goodfiles_nustar_fpm_clockcor_20230516.tar.gz<br><br>#CIAO<br>wget -c https://cxc.cfa.harvard.edu/cdaftp/arcftp/caldb/caldb_4.10.4_main.tar.gz<br>wget -c https://cxc.cfa.harvard.edu/cdaftp/arcftp/caldb/acis_bkgrnd_4.9.7.tar.gz<br>wget -c https://cxc.cfa.harvard.edu/cdaftp/arcftp/caldb/hrc_bkgrnd_4.7.7.tar.gz<br>wget -c https://cxc.cfa.harvard.edu/cdaftp/arcftp/caldb/upgrade/caldb_4.10.4_upgrade.tar.gz</pre><p>Now set the local calibration database as $CALDB=$HEASTRO_PATH/CALDB and extract the downloaded files to $CALDB. This can be done by</p><pre>tar -zxvf goodfiles_nustar_fpm_20230516.tar.gz --directory $CALDB<br>tar -zxvf goodfiles_nustar_fpm_clockcor_20230516.tar.gz --directory $CALDB<br>tar -zxvf caldb_4.10.4_main.tar.gz --directory $CALDB<br>tar -zxvf acis_bkgrnd_4.9.7.tar.gz --directory $CALDB<br>tar -zxvf hrc_bkgrnd_4.7.7.tar.gz --directory $CALDB<br>tar -zxvf caldb_4.10.4_upgrade.tar.gz --directory $CALDB</pre><p>Notice that the calibration data will now have the structure $CALDB/data/&lt;mission&gt;/&lt;instrument&gt;, i.e. for NuStar the calibration database will be $CALDB/data/nustar/fpm, while for the Chandra ACIS instrument, it will be $CALDB/data/chandra/acis. You can safely ignore the docs, and software directories in there for now. A good way to check the status of the local calibration database at this point is to check the links to the index files by</p><pre>(heastro) user@host:/disk2/user/heastro_software$ ls -l caldb/data/*/*/caldb.indx<br>lrwxrwxrwx 1 user group 23 Feb 23 05:34 caldb/data/chandra/acis/caldb.indx -&gt; ./index/caldbN0464.indx<br>lrwxrwxrwx 1 user group 23 Apr 16  2022 caldb/data/chandra/default/caldb.indx -&gt; ./index/caldbN0407.indx<br>lrwxrwxrwx 1 user group 23 Jul 29  2014 caldb/data/chandra/ephin/caldb.indx -&gt; ./index/caldbN0400.indx<br>lrwxrwxrwx 1 user group 23 Apr 22  2022 caldb/data/chandra/hrc/caldb.indx -&gt; ./index/caldbN0436.indx<br>lrwxrwxrwx 1 user group 23 Feb  7 05:09 caldb/data/chandra/pcad/caldb.indx -&gt; ./index/caldbN0408.indx<br>lrwxrwxrwx 1 user group 23 Nov 11  2022 caldb/data/chandra/pimms/caldb.indx -&gt; ./index/caldbN0415.indx<br>lrwxrwxrwx 1 user group 23 Jul 29  2014 caldb/data/chandra/sim/caldb.indx -&gt; ./index/caldbN0400.indx<br>lrwxrwxrwx 1 user group 24 May 16 23:22 caldb/data/nustar/fpm/caldb.indx -&gt; index/caldb.indx20230516<br>(heastro) user@host:/disk2/user/heastro_software$ </pre><p>This shows you that the calibration data for the different instruments missions are properly indexed. However, if you run caldbinfo (remember that heasoft is already installed and so caldbinfo is available) at this stage for any instrument and mission at this point, you will get the following error message:</p><pre>(heastro) user@host:/disk2/user/heastro_software$ caldbinfo chandra sim<br> ...  using gpcaldbinfo 1.0.0<br> ** caldbinfo 1.0.2<br> ...  using infocaldb   1.0.1<br> ERROR - caldb_info 1.3: CALDB environment variable not set<br> ... The environ-var/logical CALDB must be set to point to the top of the Caldb<br>     directory structure<br> ... See the Caldb Users Guide (CAL/GEN/94-002) for details<br> ERROR - caldb_info 1.3: Serious problem with CALDB set-up<br> ERROR - infocaldb 1.0.1: CALDB not defined/available<br> ... Task requires CALDB to be both defined &amp; available in order to run<br> ERROR - infocaldb 1.0.1: Aborting<br> ERROR - caldbinfo 1.0.2: INCOMPLETE EXECUTION<br> ** caldbinfo 1.0.2 stop<br> caldbinfo 1.0.2 : FAILED<br>(heastro) user@host:/disk2/user/heastro_software$ </pre><p>So what happened here? We have only correctly setup the calibration indices for the instruments and missions. Also, we see that the $CALDB environment variable is not available to caldbinfoyet. So we still need to install the CALDB. The way to do this would be (following the <a href="http://heasarc.gsfc.nasa.gov/docs/heasarc/caldb/install.html">installation instructions</a>) to download the HEASARC’s caldb installation <a href="http://heasarc.gsfc.nasa.gov/FTP/caldb/software/tools/caldb_setup_files.tar.Z">software</a> and extract the archive in $CALDB, which will create two files for our use in $CALDB/software/tools: caldb.config and caldbinit.sh. Edit the top lines in these two files to reflect the $CALDB location and add the following lines in /home/user/.bashrc:</p><pre>export CALDB=/disk2/user/heastro_software/caldb<br>source $CALDB/software/tools/caldbinit.sh</pre><p>Run source /home/user/.bashrc. Check that the $CALDB has been correctly setup to point to caldb by issuing $CALDB at the prompt. Now rerun cadldbinfo chandra sim. You can equivalently try caldbinfo nustar fpm or caldb chandra acis. The output should now be</p><pre>(heastro) user@host:~$ caldbinfo nustar fpm<br> ...  using gpcaldbinfo 1.0.0<br> ** caldbinfo 1.0.2<br> ...  using infocaldb   1.0.1<br> ...... environ-var/logical CALDB defined<br> .........   CALDB path = /disk2/user/heastro_software/caldb<br> ... environ-var/logical CALDBCONFIG defined<br> ...   CALDBCONFIG file = /disk2/user/heastro_software/caldb/software/tools/c<br>     aldb.config<br> ...... environ-var/logical CALDBALIAS defined<br> .........   CALDBALIAS file = /disk2/user/heastro_software/caldb/software/to<br>           ols/alias_config.fits<br> ... CALDB appears to be set-up &amp; accessible<br> ** caldbinfo 1.0.2 completed successfully<br>(heastro) user@host:~$</pre><p>We now have caldb has been setup, and will be accessible every time a new terminal is started irrespective of the conda env. So let’s now go back to our example observation and check that the basic data reduction pipeline for this observation works. The <a href="https://heasarc.gsfc.nasa.gov/docs/nustar/analysis/nustar_swguide.pdf">detailed manual</a> for NuStar data reduction should be read for scientific analysis. But a brief overview of the data reduction pipeline is shown here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9-RiC6P793sM5ZzKGFUsPw.png" /><figcaption>Data reduction pipeline for NuStar using HEASoft. Adapted from the data analysis software guide.</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ec54207b832" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sovereign Compilers: Building a Fully Static, Modern LLVM 19 on macOS Ventura in 2026]]></title>
            <link>https://medium.com/@sayantanghosh_38070/sovereign-compilers-building-a-fully-static-modern-llvm-19-on-macos-ventura-in-2026-ca0fdff131e2?source=rss-b977d596d5b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca0fdff131e2</guid>
            <category><![CDATA[optimised-build]]></category>
            <category><![CDATA[static]]></category>
            <category><![CDATA[clang-llvm]]></category>
            <category><![CDATA[macos-ventura]]></category>
            <category><![CDATA[simd]]></category>
            <dc:creator><![CDATA[Sayantan Ghosh]]></dc:creator>
            <pubDate>Sat, 14 Feb 2026 17:09:10 GMT</pubDate>
            <atom:updated>2026-02-15T07:45:30.771Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sovereign Compilers: Building a Fully Static, Modern LLVM 19 on macOS Ventura in 2026 (yes, it still works)</h3><p>Ventura is old, and hardly supported anymore, either by Apple (which makes sense), or by homebrew. In fact the latest compatible Apple SDK is 14.2, but while support from enhancements may not be apparently available, it doesn’t mean that the older generation cannot be tuned to work better. More so the case for older generation intel based MacBookPro. Since package manager based software binaries are designed to be as widely accessible as possible, they are often not tuned to specific systems’ architectures or user needs.</p><p>Here, we do something a little radical, build a fully static modern LLVM-19 system with clang and flang compilers and openmp support on an old system with C++23 support and one that is fully SIMD optimised (i.e. the compiler and library themselves are architecture optimised). While we do this on an intel MacBookPro 2017 with an Intel-i5 (haswell) processor running Ventura 13.7.8, this method can be adapted to work on any UNIX/Linux system. across any Stay tuned!</p><p>The system specifications we tried the experiment on are:</p><pre>xcodebuild -version<br>Xcode 15.2<br>Build version 15C500b<br>$ xcrun clang -v<br>Apple clang version 15.0.0 (clang-1500.1.0.2.5)<br>Target: x86_64-apple-darwin22.6.0<br>Thread model: posix<br>InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin</pre><p>Building LLVM on macOS is usually straightforward until you try to make it <strong>sovereign</strong>. What we mean by sovereign is that the LLVM is as standalone as possible, without having to load multiple libraries at runtime, and with minimal interaction with the system libraries. By default, clang is designed to link against system dynamic libraries(.dylibs) for it’s dependencies: zlib, libxml2, and others. In order to build a high-performance, architecturally optimised (Haswell/AVX2) toolchain that carries its own weight, we have to build the entire dependency stack from source and force a 2-stage bootstrap to respect those static roots.</p><p>Here is the blueprint for building a “Sovereign” LLVM 19.</p><h3>Dependencies</h3><p>We build everything with CFLAGS and CXXFLAGS set to -O3 -march=haswell -fPIC -mmacosx-version-min=13.7. This does three things:</p><ul><li>asks the compiler to aggressively transform every instruction and loop to SIMD instructions for the specific CPU architecture,</li><li>builds it as Position Independent Code (PIC), which is more robust for shared libraries. You can read more about PIC <a href="https://en.wikipedia.org/wiki/Position-independent_code">here</a>.</li><li>and ensures through the -mmacosx-version-min=13.7 ensures that the clang is built for only ventura (13.7), it can be changed according to needs. The reason for this flag is that the current Apple SDK on Ventura is 14.2 which if future-looking, allowing for may preview features of the later Sonoma to be inbuilt into softwares and libraries, which can cause inconsistencies in software dependencies making software builds get corrputed. As an additional safety feature, we also set the environment variable MACOSX_DEPLOYMENT_TARGET=13.7. We build the following dependencies, and make sure that we are building them as static (.a) . Don’t worry if some of the dynamic library files (.dylib) are installed too, we will try to use only the static libraries.</li></ul><p>Note:</p><ul><li>I don’t like the bloat that comes with GNU when I don’t use GNU at all, so we will have minimal dependence on GNU libaries (bison, readline, editline). We will try to use cmake and ninja as much as we can. I will also assume that standalone cmake and ninja are available on the system.</li><li>The package versions that I have used here are the just my preferred versions (I found them to be stable and/or latest compatible for my system) as of January 2026. Newer package versions will work too and anyone wanting to build on their particular operating systems at a different time can check for their optimal packages.</li></ul><p>First let’s setup the flags:</p><pre>export CC=/usr/bin/clang<br>export CXX=/usr/bin/clang++<br>export MACOSX_DEPLOYMENT_TARGET=&quot;13.7&quot;<br>export CFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot;<br>export CXXFLAGS=&quot;-march=haswell -mavx2 -fPIC -std=c++2b -stdlib=libc++ -O3 -mmacosx-version-min=13.7&quot;</pre><p>Now let’s build the dependencies.</p><ul><li>re2c for converting regular expressions to C.</li></ul><pre>git clone https://github.com/skvadrik/re2c.git<br>cd re2c<br>mkdir build_sovereign &amp;&amp; cd build_sovereign<br>cmake -G Ninja .. \<br>  -DCMAKE_BUILD_TYPE=Release \<br>  -DCMAKE_INSTALL_PREFIX=/usr/local \<br>  -DRE2C_BUILD_LIBS=ON<br>ninja <br></pre><ul><li>lz4 (a lossless compression algorithm used by many modern compression libraries)</li></ul><pre>git clone https://github.com/lz4/lz4.git<br>cd lz4/build/cmake<br>cmake -G Ninja . \<br> -DCMAKE_BUILD_TYPE=Release \<br> -DCMAKE_INSTALL_PREFIX=/usr/local \<br> -DBUILD_SHARED_LIBS=OFF \ <br> -DBUILD_STATIC_LIBS=ON \ <br> -DCMAKE_POSITION_INDEPENDENT_CODE=ON<br>ninja</pre><ul><li>zlib-ng (Next generation zlib)</li></ul><p>We use zlib-ng in compat mode for better performance on modern CPUs.</p><pre>git clone https://github.com/zlib-ng/zlib-ng<br>mkdir build<br>cmake -G Ninja -S . -B build \<br> -DCMAKE_BUILD_TYPE=Release \<br> -DZLIB_COMPAT=ON \<br> -DBUILD_SHARED_LIBS=OFF \<br> -DCMAKE_INSTALL_PREFIX=/usr/local<br>ninja -C build install</pre><ul><li>iconv (a library to convert text encodings)</li></ul><pre>curl -OL https://ftp.gnu.org/gnu/libiconv/libiconv-1.18.tar.gz<br>cd libiconv-1.18/<br>make distclean || true<br>./configure --prefix=/usr/local \ <br> --enable-static \<br> --disable-shared \<br> --disable-nls \<br> --enable-extra-encodings<br>make -j$(sysctl -n hw.ncpu)<br>sudo make install</pre><ul><li>bz2 (a legacy compression algorithm still in use)</li></ul><pre>git clone git clone git://sourceware.org/git/bzip2.git<br>cd bzip2-1.0.8/<br>make distclean || true<br>make CFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot; -j$(sysctl -n hw.ncpu)<br>sudo make install PREFIX=/usr/local</pre><ul><li>libedit (we use the autotool-ized, and libtool-ized) version.</li></ul><pre>curl -OL https://thrysoee.dk/editline/libedit-20251016-3.1.tar.gz<br>tar -zxf libedit-20251016-3.1.tar.gz<br>cd libedit-20251016-3.1/<br>./configure --prefix=/usr/local <br> --enable-static \<br> --disable-shared --with-pic \<br> --CFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot;<br>make -j$(sysctl -n hw.ncpu)</pre><ul><li>readline (for an editable command line interface)</li></ul><pre>curl -OL https://ftp.gnu.org/gnu/readline/readline-8.2.tar.gz<br>cd readline-8.2<br>./configure --prefix=/usr/local \<br>  --enable-static \<br>  --disable-shared \<br>  --with-curses \<br>  --CFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot;             CPPFLAGS=&quot;-I/usr/local/include&quot;             LDFLAGS=&quot;-L/usr/local/lib&quot;<br>make -j$(sysctl -n hw.ncpu)</pre><ul><li>pcre2 (perl compatible regular expressions).</li></ul><pre>cd pcre2-10.42<br>git submodule update --init --recursive<br>mkdir build &amp;&amp; cd build<br>cmake -G Ninja .. \<br>  -DCMAKE_BUILD_TYPE=Release \<br>  -DCMAKE_INSTALL_PREFIX=/usr/local \<br>  -DBUILD_SHARED_LIBS=OFF \<br>  -DBUILD_STATIC_LIBS=ON \<br>  -DPCRE2_STATIC_PIC=ON \<br>  -DCMAKE_POSITION_INDEPENDENT_CODE=ON \<br>  -DPCRE2_SUPPORT_JIT=ON \<br>  -DPCRE2_SUPPORT_UNICODE=ON \<br>  -DREADLINE_LIBRARY=&quot;/usr/local/lib/libreadline.a&quot; \<br>  -DREADLINE_INCLUDE_DIR=&quot;/usr/local/include&quot; \<br>  -DEDITLINE_LIBRARY=&quot;/usr/local/lib/libedit.a&quot; \<br>  -DEDITLINE_INCLUDE_DIR=&quot;/usr/local/include&quot; \<br>  -DZLIB_LIBRARY=&quot;/usr/local/lib/libz.a&quot; \<br>  -DBZIP2_LIBRARY_RELEASE=&quot;/usr/local/lib/libbz2.a&quot; \ <br>  -DCMAKE_C_FLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot;<br>ninja</pre><p>ncurses-6.5 (for text based interfaces, with additional wide character support)</p><pre>cd ncurses-6.5<br>./configure --prefix=/usr/local \<br>  --enable-widec \<br>  --with-static \<br>  --with-shared \<br>  --with-pic \<br>  --without-debug \<br>  --enable-pc-files \<br>  --disable-nls \<br>  CC=&quot;clang&quot; \<br>  CXX=&quot;clang++&quot; \<br>  CFLAGS=&quot;-march=haswell -mavx2 -Ofast -fPIC -mmacosx-version-min=13.7 -mllvm -force-vector-width=8&quot; \<br>  CPPFLAGS=&quot;-I/usr/local/include&quot; \<br>  LDFLAGS=&quot;-L/usr/local/lib&quot; --disable-docs<br>make -j$(sysctl -n hw.ncpu)</pre><ul><li>xz Utils (forliblzma), a high compression ratio compression library.</li></ul><p>It is essential for LLDB and compressed debug sections.</p><pre>git clone https://github.com/tukaani-project/xz<br>git checkout v5.6.3<br>cmake -G Ninja -S . -B build \ <br>  -DCMAKE_BUILD_TYPE=Release \ <br>  -DBUILD_SHARED_LIBS=OFF \ <br>  -DCMAKE_INSTALL_PREFIX=/usr/local <br>ninja -C build install</pre><ul><li>zstd Standard for modern LLVM bitcode compression.</li></ul><pre>cmake -G Ninja -S build/cmake -B build_dir \<br> -DCMAKE_BUILD_TYPE=Release \<br> -DZSTD_BUILD_SHARED=OFF \<br> -DZSTD_BUILD_STATIC=ON \<br> -DCMAKE_INSTALL_PREFIX=/usr/local<br>ninja -C build_dir install</pre><ul><li>bison (GNU’s multi-purpose parser generator)</li></ul><pre>curl -OL https://ftp.gnu.org/gnu/bison/bison-3.8.2.tar.gz<br>tar -zxf bison-3.8.2.tar.gz <br>cd bison-3.8.2<br>./configure --prefix=/usr/local \<br>  --disable-nls \<br>  CFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -mmacosx-version-min=13.7&quot;             LDFLAGS=&quot;-L/usr/local/lib&quot;<br>make -j$(sysctl -n hw.ncpu)</pre><ul><li>swig (a software development tool used to integrate C and C++ codes with high-level programming languages)</li></ul><pre>curl -L -o swig-4.3.0.tar.gz https://downloads.sourceforge.net/swig/swig-4.3.0.tar.gz<br>tar -xf swig-4.3.0.tar.gz<br>cd swig-4.3.0/ &amp;&amp; mkdir build &amp;&amp; cd build<br>cmake -G Ninja .. \<br>  -DCMAKE_BUILD_TYPE=Release <br>  -DCMAKE_INSTALL_PREFIX=/usr/local \<br>  -DCMAKE_SKIP_INSTALL_RPATH=ON \<br>  -DCMAKE_SKIP_RPATH=ON \<br>  -DBISON_EXECUTABLE=&quot;/usr/local/bin/bison&quot; \<br>  -DPCRE2_INCLUDE_DIR=&quot;/usr/local/include&quot; \<br>  -DPCRE2_LIBRARY=&quot;/usr/local/lib/libpcre2-8.a&quot; \<br>  -DZLIB_INCLUDE_DIR=&quot;/usr/local/include&quot; \<br>  -DZLIB_LIBRARY=&quot;/usr/local/lib/libz.a&quot;  \<br>  -DCMAKE_C_FLAGS=&quot;-march=haswell -mavx2 -fPIC -O3&quot;  \<br>  -DCMAKE_CXX_FLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -std=c++2b&quot;<br>ninja </pre><ul><li>icu4c (the International Components for Unicode library)</li></ul><pre>curl -L -O https://github.com/unicode-org/icu/releases/download/release-76-1/icu4c-76_1-src.tgz<br>tar -zxf icu4c-76_1-src.tgz <br>cd icu/source/<br>CXXFLAGS=&quot;-march=haswell -mavx2 -fPIC -O3 -std=c++2b -mmacosx-version-min=13.7&quot;<br>export CPPFLAGS=&quot;-DU_STATIC_IMPLEMENTATION&quot;<br>export LDFLAGS=&quot;-L/usr/local/lib&quot;<br>./runConfigureICU MacOSX \<br>  --prefix=/usr/local \<br>  --enable-static \<br>  --disable-shared  \<br>  --disable-samples \<br>  --disable-tests \<br>  --disable-extras \<br>  --with-data-packaging=static<br>make -j$(sysctl -n hw.ncpu)</pre><ul><li>The super static libXML2 (for parsing, creating, and manipulating XML and HTML codes).</li></ul><p>On macOS, libxml2 typically wants to dynamic-link against libiconv, libz, and liblzma. To achieve true sovereignty, we re-configured libxml2 so that its static archive effectively “consumed” its dependencies, or at least ensured they were resolved correctly during our final link without polluting the Stage 2 variable list.</p><pre>curl -OL https://download.gnome.org/sources/libxml2/2.13/libxml2-2.13.5.tar.xz<br>tar -zxf libxml2-2.13.5.tar.xz<br>cd libxml2-2.13.5/<br>cmake -G Ninja -S . -B build \<br> -DCMAKE_BUILD_TYPE=Release \<br> -DBUILD_SHARED_LIBS=OFF \<br> -DLIBXML2_WITH_ICONV=ON \<br> -DLIBXML2_WITH_LZMA=ON \<br> -DLIBXML2_WITH_ZLIB=ON \<br> -DLIBXML2_WITH_PYTHON=OFF \<br> -DICONV_LIBRARY=/usr/local/lib/libiconv.a \<br> -DICONV_INCLUDE_DIR=/usr/local/include \<br> -DCMAKE_INSTALL_PREFIX=/usr/local <br>ninja -C build install</pre><p>Note: By resolving these at the LibXml2 level, we simplified the LLVM Stage 2 configuration, avoiding the dreaded semicolon-delimited string errors in Ninja.</p><p>Just to show how the core of our system works, we can perform a few checks.</p><ol><li>Check for architecture optimisation in libxml2:</li></ol><pre>otool -tV /usr/local/lib/libxml2.a | grep -E &quot;vpbroadcast|vpand|vmovdqu&quot; | head -5<br>0000000000000321 vmovdqu 0x20(%rdi), %xmm0<br>0000000000000765 vmovdqu 0x20(%rdi), %xmm0<br>0000000000000be0 vmovdqu 0x20(%r14), %xmm0<br>0000000000000cf8 vpbroadcastd 0x2af(%rip), %xmm1<br>0000000000007aa6 vpand 0x2792(%rip), %xmm0, %xmm0</pre><p>This shows that the libxml2 library has inbuilt architecture optimisation. Which means any use of this library will also be fast and optimised. Not just libxml2 , let’s check for one of the first libraries we built: re2c:</p><pre>otool -tV /usr/local/bin/re2c | grep -E &quot;vpbroadcast|vpand|vmovdqu&quot; | head -5<br>0000000100026f1a vmovdqu 0xa3747(%rip), %xmm0            ## literal pool for: &quot;&lt;default syntax file&gt;&quot;<br>0000000100026f22 vmovdqu %xmm0, -0x3f(%rbp)<br>0000000100026f4b vmovdqu -0x40(%rbp), %xmm0<br>0000000100026f50 vmovdqu %xmm0, (%rax)<br>0000000100027036 vpbroadcastq %xmm0, %ymm0</pre><p>Any other library or package that we have already built is now architecture optimised!!! That makes sure that there are no performance bottlenecks in our compiler.</p><h3>LLVM — 19 build</h3><p>The challenge with a 2-stage LLVM build is “the hand-off.” We must ensure Stage 2 doesn’t “forget” our static paths and revert to the macOS SDK. We perform three things here:</p><ul><li><strong>Prefix Injection:</strong> Use -DBOOTSTRAP_… variables to let the Stage 1 CMake automatically pass paths to Stage 2.</li><li><strong>Semicolon Avoidance:</strong> For complex library lists (like LibXml2 + IconV + Zlib), pass the main library in the variable and tuck the rest into CMAKE_EXE_LINKER_FLAGS.</li><li><strong>The Terminal Glue:</strong> Always include libncursesw.a in the linker flags to satisfy libedit.</li></ul><p>And now, finally, the Sovereign Command</p><pre>cmake -G Ninja ../llvm \<br> -DCLANG_ENABLE_BOOTSTRAP=ON \<br> -DCMAKE_BUILD_TYPE=Release \<br> -DCMAKE_INSTALL_PREFIX=/usr/local/llvm-19 \<br> -DCMAKE_OSX_SYSROOT=&quot;$(xcrun --show-sdk-path)&quot; \<br> -DLLVM_ENABLE_PROJECTS=&quot;clang;lld&quot; \<br> -DLLVM_ENABLE_RUNTIMES=&quot;compiler-rt;libunwind;libcxxabi;libcxx&quot; \<br> -DLLVM_TARGETS_TO_BUILD=&quot;X86&quot; \<br> -DCMAKE_C_FLAGS=&quot;-O3 -march=haswell -mavx2 -msse4.2 -fPIC&quot; \<br> -DCMAKE_CXX_FLAGS=&quot;-O3 -march=haswell -mavx2 -msse4.2 -fPIC -std=c++2b&quot; \<br> -DCMAKE_EXE_LINKER_FLAGS=&quot;/usr/local/lib/libiconv.a /usr/local/lib/libz.a /usr/local/lib/liblzma.a /usr/local/lib/libncursesw.a&quot; \<br> -DLLVM_ENABLE_ZLIB=FORCE_ON \<br> -DZLIB_LIBRARY=&quot;/usr/local/lib/libz.a&quot; \<br> -DLLVM_ENABLE_LIBXML2=FORCE_ON \ <br> -DLIBXML2_LIBRARY=&quot;/usr/local/lib/libxml2.a&quot;\<br> -DBOOTSTRAP_CMAKE_PREFIX_PATH=&quot;/usr/local&quot; \<br> -DCLANG_BOOTSTRAP_CMAKE_ARGS=&quot;\<br>  -DLLVM_ENABLE_PROJECTS=clang;clang-tools-extra;lld;lldb;polly;mlir;flang;\<br>  -DLLVM_ENABLE_RUNTIMES=compiler-rt;libunwind;libcxxabi;libcxx;openmp;\<br>  -DLIBXML2_LIBRARY=/usr/local/lib/libxml2.a;\<br>  -DZLIB_LIBRARY=/usr/local/lib/libz.a;\<br>  -DLIBLZMA_LIBRARY=/usr/local/lib/liblzma.a;\<br>  -DLibEdit_LIBRARIES=/usr/local/lib/libedit.a;\<br>  -DCMAKE_CXX_FLAGS=-O3 -march=haswell -mavx2 -msse4.2 -fPIC -std=c++23 -stdlib=libc++ -DU_STATIC_IMPLEMENTATION;\<br>  -DCMAKE_EXE_LINKER_FLAGS=/usr/local/lib/libiconv.a /usr/local/lib/libz.a /usr/local/lib/liblzma.a /usr/local/lib/libncursesw.a /usr/local/lib/libpanelw.a&quot;</pre><pre>ninja stage2 </pre><ul><li>Check for sovereignty:</li></ul><p>After the 8,000+ target build completes, we verify the “Sovereignty”:</p><pre>otool -L /usr/local/llvm-19/bin/clang-19<br>/usr/local/llvm-19/bin/clang-19: /usr/lib/libSystem.B.dylib <br>/usr/lib/libc++.1.dylib</pre><p>Let’s also</p><p>No libz.dylib. No libxml2.dylib. Just clean, static, AVX2-optimised power!!!!</p><p><strong>A short summary of what happened up in the sovereign command:</strong></p><p>Compiler Sovereignty: We used BOOTSTRAP_CMAKE_PREFIX_PATH=&quot;/usr/local&quot; to force the second stage to look in our custom directory before it looks in the macOS SDK. This is what prevents the build from accidentally linking against the system&#39;s .tbd stubs.</p><p>When you run the command, the hand-off looks like this:</p><ul><li><strong>Stage 1</strong>: -DBOOTSTRAP_LibEdit_LIBRARIES=/usr/local/lib/libedit.a The Translation: clang/CMakeLists.txt sees the prefix.</li><li><strong>Stage 2 Execution</strong>: cmake ../llvm -DLibEdit_LIBRARIES=/usr/local/lib/libedit.a ...</li></ul><p>This ensures that the compiler being built is exactly as optimised and independent as the compiler building it. Passing -std=c++23 during the bootstrap wasn&#39;t just about using the latest version; it&#39;s about a paradigm shift in performance. With C++23, we are seeing the language evolve to meet the needs of modern HPC (High-Performance Computing) i.e., it simplifies generic code and reduces template bloat, leading to smaller, faster binaries.</p><p>Note that we passed two different flags for the C++ standard:</p><ul><li>-std=c++2b for Apple clang, and</li><li>-std=c++23 for LLVM clang.</li></ul><p>This is just how the Apple SDK 14.2 sets the C++23 flag (since my specific version of the SDK was released before the C++23 standard), and if the stage 2 of the bootstrap indeed was using the Apple clang it would have thrown an error on compiler check.</p><p>Just a sidebar on a cool feature in C++23,std::expected and std::optional extensions: Functional-style error handling that allows the compiler to optimise branch prediction much more effectively than traditional try-catch blocks.</p><p>And, just to check if the compiler itself is optimised,</p><pre> otool -tV /usr/local/llvm-19/bin/clang | grep -E &quot;vpbroadcast|vpand|vmovdqu&quot; | head -5<br>000000010001a81e vpbroadcastw %xmm0, %ymm0<br>000000010001aab6 vmovdqu (%rsi), %ymm0<br>000000010001aaba vmovdqu %ymm0, (%rbx)<br>000000010001aad0 vmovdqu (%rsi,%rax), %ymm0<br>000000010001aad5 vmovdqu %ymm0, (%rbx,%rax)</pre><p><strong>Standard Library Modules:</strong> By enabling import std;, we move away from the 50-year-old #include model. For a massive project like LLVM, this can theoretically reduce compile times by orders of magnitude while allowing the compiler to perform better global analysis of the standard library code.</p><p>Building LLVM with C++23 and -march=haswell (at least) creates a toolchain where the compiler &quot;understands&quot; the hardware it lives on, utilizing the latest language constructs to squeeze every cycle out of the AVX2 and FMA units. Since I have haswell, I used that, but any modern intel architecture will also work. You should avoid using -march=native, since it does optimise as well for the processor architecture.</p><p>We didn’t just build a compiler; we built an environment. By merging our static dependencies, clearing the Modules confusion, and embracing C++23, we’ve moved past the <strong>it works on my machine</strong> phase and into the <strong>this machine is a high-performance sanctuary</strong> phase.</p><p>And this should serve as a base for high — performance computing even on older systems.</p><p><em>Originally published at </em><a href="https://gist.github.com/sayantanghosh/52b7044c7915ffb2ea56da97b86abeac"><em>http://github.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca0fdff131e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>