Artistic Style

From HalfgeekKB
Jump to navigation Jump to search

Artistic Style (command: astyle) is a source formatter for C-like programming languages.

Figuring out the right settings

UniversalIndentGUI is a GUI application that allows you to try out many of the various settings provided by astyle (and several other formatters). The program is a little old and probably doesn't support all the newer features, but it's a decent starting point. It allows you to save/load the settings as an .astylerc file.

Loading options from a file

 astyle --options=ASTYLERC_FILE < SOURCE_FILE > OUTPUT_FILE

Example configurations

Personal preferences for Arduino

--style=linux
--indent-classes
--indent-switches
--indent-cases
--indent-namespaces
--pad-oper
--pad-header
--unpad-paren
-y
--keep-one-line-blocks
--keep-one-line-statements
--indent=spaces=2
--align-pointer=middle
--align-reference=middle
--mode=c

Installation

Because new and desirable features are added way before your distro picks them up in the standard package, this is a program you'll want to compile and install manually.

The program unfortunately has a static Makefile and doesn't use a configure script, so using stow is a bit more difficult than normal.

We use almost the normal mkfake process here, with these subtleties:

  • The Makefile will have to be patched so that DESTDIR is supported.
  • The prefix /usr/local will be passed directly to make as a variable.

Unpack

Start by unpacking and going to the build dir for gcc.

# In /tmp (substitute another dir as appropriate)
$ tar xvf ~/src/astyle_3.0.1_linux.tar.gz
$ cd astyle/build/gcc

Add DESTDIR support in Makefile

In the Makefile, any destination directories under only the install and uninstall targets need to be prefixed with $(DESTDIR) and quoted as necessary. As of astyle 3.0.1, this is done with the following changes:

  • Replace any $(ipath) with "$(DESTDIR)$(ipath)".
  • Replace any $(SYSCONF_PATH) with "$(DESTDIR)$(SYSCONF_PATH)".

My diff for astyle 3.0.1 looked like this:

--- Makefile.original	2017-10-20 11:04:21.741319875 -0400
+++ Makefile	2017-10-20 11:05:29.609323159 -0400
@@ -193,20 +193,20 @@
 	rm -f $(objdir)/*.o
 
 install:
-	$(INSTALL) -m 755 -d $(ipath)
-	@$(INSTALL) -m 755 $(bindir)/astyle  $(ipath)
+	$(INSTALL) -m 755 -d "$(DESTDIR)$(ipath)"
+	@$(INSTALL) -m 755 $(bindir)/astyle  "$(DESTDIR)$(ipath)"
 
-	@if [ -d $(SYSCONF_PATH)/html ]; then \
-		rm -rf  $(SYSCONF_PATH)/html; \
+	@if [ -d "$(DESTDIR)$(SYSCONF_PATH)"/html ]; then \
+		rm -rf  "$(DESTDIR)$(SYSCONF_PATH)"/html; \
 	fi
 
-	$(INSTALL) -m 755 -d $(SYSCONF_PATH)
-	@mkdir -p $(SYSCONF_PATH)/html;
+	$(INSTALL) -m 755 -d "$(DESTDIR)$(SYSCONF_PATH)"
+	@mkdir -p "$(DESTDIR)$(SYSCONF_PATH)"/html;
 	@for files in ../../doc/*.html  ../../doc/*.css; \
 	do \
-		$(INSTALL)  -m 644  $$files  $(SYSCONF_PATH)/html; \
+		$(INSTALL)  -m 644  $$files  "$(DESTDIR)$(SYSCONF_PATH)"/html; \
 	done
 
 uninstall:
-	rm -f $(ipath)/astyle
-	rm -rf $(SYSCONF_PATH)
+	rm -f "$(DESTDIR)$(ipath)"/astyle
+	rm -rf "$(DESTDIR)$(SYSCONF_PATH)"

Create fake install dir with mkake

Use mkfake to set up a fake destination directory. mkfake arranges for the install prefix under the fake dir to link to a directory with the name of the package (in this case astyle-3.0.1); this directory will be stowed later.

$ eval "`PACKAGE=astyle-3.0.1 mkfake`"
# Agree to prompts - refer to mkfake for more information

Build and install to fake

Use make to build and install the program. Note that make install won't work on its own; one of the astyle build targets (such as release) must be built first.

This invocation of make is also where prefix and DESTDIR must be set. mkfake has added the correct values in the environment as REAL and FAKE, respectively.

# Build the release version and install it to the fake dir
$ make prefix="$REAL" DESTDIR="$FAKE" release install

Move the fake install dir into /usr/local/stow and stow it

Once that's done, stow the result as with any mkfake'd build.

# Install the build to the stow dir and stow it
cd /usr/local/stow
sudo mv "$FAKE/$PACKAGE" ./
sudo stow "$PACKAGE"