%\documentclass{article}
%\usepackage[legalpaper, margin=1in]{geometry}
\newcommand{\code}[1]{\texttt{#1}}


%\begin{document}
%Instructions for MacOS installation:
\subsection{Tips for MacOS installation}
\textit{By Deepen Garg} (\url{https://www.dgarg.com/})

\begin{enumerate}
\item Download Xcode Command Line Tools, by terminal command:
%
\begin{verbatim}
    xcode-select --install
\end{verbatim}
%
or, if that doesn't work, by downloading from \code{https://developer.apple.com/download/all/}. Version 15.4 has been seen to run on Macbooks with the Apple M series chips as well as the Intel chips (you can check the chip in \textit{About This Mac} from the Apple icon on the top left). 
You do not need the full Xcode, but that should work as well   %MR: what is meant?
(Be careful with the large install size of the full Xcode). This step installs \code{git} and a C compiler along with some other tools. The default C compiler that ships with v15.4 is \code{clang} (you can check the version with the command: \code{clang --version}). For internal purposes, Apple, however, also ``installs" another compiler called \code{gcc} which is running \code{clang} under the hood. You can check this with: 
%the command: 
\code{gcc --version}, which should show the same result as the clang version. 

\item Install Homebrew by running the following command in the terminal (check with \code{https://brew.sh/} for the latest installation steps):
%
\begin{verbatim}
/bin/bash -c "$(curl -fsSL 
    https://raw.githubusercontent.com/
    Homebrew/install/HEAD/install.sh)"
\end{verbatim}
%
Homebrew is a package manager for MacOS (and Linux), which we are going to use to obtain the various compilers required in the following steps.

\item Install C and Fortran compilers using the command \code{brew install gcc}. The {gcc} package contains various GNU compilers. The ones we need are \code{gfortran} for Fortran, and \code{gcc} for C (Be careful not to confuse the gcc package with the gcc compiler). However, the gcc compiler installed by brew is actually not called \code{gcc} because of the conflict with \code{gcc} distributed by Apple. They are usually called as \code{gcc-xx} where xx represents the major version number. Checking the \code{gfortran} version number (\code{gfortran --version}) should show Homebrew as the source and the major version number. Then, running \code{gcc-xx --version} should also show the same version number as \code{gfortran} and Homebrew as the source.

\item Install the openmpi compiler 
%. This installs mpi compilers 
required for parallelization
by running \code{brew install open-mpi}

\item If you are lucky, this setup should work as-is. However, there have been multiple reports of it not working, and the culprit seems to be the fact that Pencil Code (PC) uses the gcc compiler, which is \code{clang} under the hood for Macs, and the gfortran compiler, which is installed from Homebrew, and these two compilers from different sources are not compatible sometimes. So we need to make PC use the gcc-xx compiler that was also sourced from Homebrew. There are a few solutions 
%we can adopt, 
depending upon the needs of the user:
\begin{enumerate}
\item Open the \texttt{GNU-gcc.conf} file\footnote{\texttt{\$PENCIL\_HOME/config/compilers/separate/GNU-gcc.conf}} in any text editor and change the line \code{CC = gcc} to \code{CC = gcc-xx} where xx is the version number you found in step 3 above. This will change the compiler PC is using, and everything should work fine again now. When you pull a newer version of PC, there is a small chance that this file could be overwritten, in which case you would need to change this again.
\item Check the directory listed when running \code{which gfortran} and make sure that this directory comes before the directory listed by \code{which gcc} when you run \code{echo \$PATH}. 
If required, change the \code{\$PATH} variable to make sure this is the case. Then, run this command:
%
\begin{verbatim}
cd `dirname \`which gfortran\`` &&
ln -s `ls -1 --color=never
   gcc-* | grep -Ev --color=never
   "\-[^\-\s]+\-" | tail -1` gcc
\end{verbatim}
%
This command navigates to the directory where \code{gfortran} is installed (basically the Homebrew directory) and creates a soft link called \code{gcc} for the last version of gcc it can find. Alternatively, you can do it manually by navigating to the directory listed under \code{which gfortran} and then running \code{ln -s gcc-xx gcc}.
%(the first letter is lowercase L). 
Restart your shell by either closing and opening the terminal or running \code{exec zsh} (or \code{exec bash} if you are using bash), and then run \code{gcc --version}. It should now show Homebrew as the source.
\end{enumerate}

Run PC again; this should resolve the compilation issues.

\end{enumerate}

%\end{document}
