Difference between revisions of "Artistic Style"

From HalfgeekKB
Jump to navigation Jump to search
(Created page with "'''Artistic Style''' (command: <code>astyle</code>) is a source formatter for C-like programming languages. ==Installation== Because new and desirable features are added way...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''Artistic Style''' (command: <code>astyle</code>) is a source formatter for C-like programming languages.
 
'''Artistic Style''' (command: <code>astyle</code>) is a source formatter for C-like programming languages.
 +
 +
* [http://astyle.sourceforge.net/ Project]
 +
* [http://astyle.sourceforge.net/astyle.html Full documentation]
 +
 +
==Figuring out the right settings==
 +
 +
[http://universalindent.sourceforge.net/ 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 <code>.astylerc</code> file.
 +
 +
==Loading options from a file==
 +
 +
  astyle --options=''ASTYLERC_FILE'' < ''SOURCE_FILE'' > ''OUTPUT_FILE''
 +
 +
==Example configurations==
 +
 +
===Personal preferences for Arduino===
 +
 +
<nowiki>
 +
--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</nowiki>
  
 
==Installation==
 
==Installation==
Line 11: Line 43:
 
* The Makefile will have to be patched so that <code>DESTDIR</code> is supported.
 
* The Makefile will have to be patched so that <code>DESTDIR</code> is supported.
 
* The prefix /usr/local will be passed directly to make as a variable.
 
* 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.
 
Start by unpacking and going to the build dir for gcc.
Line 19: Line 53:
 
$ cd astyle/build/gcc</nowiki>
 
$ cd astyle/build/gcc</nowiki>
  
In the Makefile, any destination directories ''under '''only''' the <code>install</code> and <code>uninstall</code> targets'' need to be prefixed with <code>$(DESTDIR)</code> and quoted as necessary. Currently, this is done with the following changes:
+
===Add DESTDIR support in Makefile===
 +
 
 +
In the Makefile, any destination directories ''under '''only''' the <code>install</code> and <code>uninstall</code> targets'' need to be prefixed with <code>$(DESTDIR)</code> and quoted as necessary. As of astyle 3.0.1, this is done with the following changes:
  
 
* Replace any <code>$(ipath)</code> with <code>"$(DESTDIR)$(ipath)"</code>.
 
* Replace any <code>$(ipath)</code> with <code>"$(DESTDIR)$(ipath)"</code>.
 
* Replace any <code>$(SYSCONF_PATH)</code> with <code>"$(DESTDIR)$(SYSCONF_PATH)"</code>.
 
* Replace any <code>$(SYSCONF_PATH)</code> with <code>"$(DESTDIR)$(SYSCONF_PATH)"</code>.
 +
 +
My diff for astyle 3.0.1 looked like this:
 +
 +
<syntaxhighlight lang=diff>
 +
--- 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)"
 +
</syntaxhighlight>
 +
 +
===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 <code>astyle-3.0.1</code>); this directory will be [[stow]]ed later.
 
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 <code>astyle-3.0.1</code>); this directory will be [[stow]]ed later.
Line 29: Line 104:
 
$ eval "`PACKAGE=astyle-3.0.1 mkfake`"
 
$ eval "`PACKAGE=astyle-3.0.1 mkfake`"
 
# Agree to prompts - refer to </nowiki>[[mkfake]]<nowiki> for more information</nowiki>
 
# Agree to prompts - refer to </nowiki>[[mkfake]]<nowiki> for more information</nowiki>
 +
 +
===Build and install to fake===
  
 
Use make to build and install the program. Note that <code>make install</code> won't work on its own; one of the astyle build targets (such as <code>release</code>) must be built first.
 
Use make to build and install the program. Note that <code>make install</code> won't work on its own; one of the astyle build targets (such as <code>release</code>) must be built first.
Line 37: Line 114:
 
# Build the release version and install it to the fake dir
 
# Build the release version and install it to the fake dir
 
$ make prefix="$REAL" DESTDIR="$FAKE" release install</nowiki>
 
$ make prefix="$REAL" DESTDIR="$FAKE" release install</nowiki>
 +
 +
===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.
 
Once that's done, [[stow]] the result as with any mkfake'd build.

Latest revision as of 09:16, 20 October 2017

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"