selftests: alsa - move shared library configuration code to conf.c
authorJaroslav Kysela <perex@perex.cz>
Tue, 29 Nov 2022 08:53:06 +0000 (09:53 +0100)
committerTakashi Iwai <tiwai@suse.de>
Tue, 29 Nov 2022 14:05:18 +0000 (15:05 +0100)
The minimal alsa-lib configuration code is similar in both mixer
and pcm tests. Move this code to the shared conf.c source file.

Also, fix the build rules inspired by rseq tests. Build libatest.so
which is linked to the both test utilities dynamically.

Also, set the TEST_FILES variable for lib.mk.

Cc: linux-kselftest@vger.kernel.org
Cc: Shuah Khan <shuah@kernel.org>
Reported-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
Tested-by: Mark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/20221129085306.2345763-1-perex@perex.cz
Signed-off-by: Takashi Iwai <tiwai@suse.de>
tools/testing/selftests/alsa/Makefile
tools/testing/selftests/alsa/alsa-local.h
tools/testing/selftests/alsa/conf.c
tools/testing/selftests/alsa/mixer-test.c
tools/testing/selftests/alsa/pcm-test.c

index 8ac22d2eb2ba77735ce5097397687676d7c6bc34..a8c0383878d3e60955c3f446ca465f22a1d38bbd 100644 (file)
@@ -6,9 +6,20 @@ LDLIBS += $(shell pkg-config --libs alsa)
 ifeq ($(LDLIBS),)
 LDLIBS += -lasound
 endif
+CFLAGS += -L$(OUTPUT) -Wl,-rpath=./
+
+OVERRIDE_TARGETS = 1
 
 TEST_GEN_PROGS := mixer-test pcm-test
 
-pcm-test: pcm-test.c conf.c
+TEST_GEN_PROGS_EXTENDED := libatest.so
+
+TEST_FILES := conf.d
 
 include ../lib.mk
+
+$(OUTPUT)/libatest.so: conf.c alsa-local.h
+       $(CC) $(CFLAGS) -shared -fPIC $< $(LDLIBS) -o $@
+
+$(OUTPUT)/%: %.c $(TEST_GEN_PROGS_EXTENDED) alsa-local.h
+       $(CC) $(CFLAGS) $< $(LDLIBS) -latest -o $@
index fd0771ac600d138450ac7278dae825bf8ea94356..65f197ea9773ab699a59fb9d26444e689e0c941d 100644 (file)
@@ -10,6 +10,8 @@
 
 #include <alsa/asoundlib.h>
 
+snd_config_t *get_alsalib_config(void);
+
 void conf_load(void);
 void conf_free(void);
 snd_config_t *conf_by_card(int card);
index dd6aa90deef9dbceaa17355c114db4d966c80a50..c7ffc8f04195511d6da33a630fedfd40ba2b6a03 100644 (file)
@@ -28,6 +28,75 @@ struct card_data {
 
 static struct card_data *conf_cards;
 
+static const char *alsa_config =
+"ctl.hw {\n"
+"      @args [ CARD ]\n"
+"      @args.CARD.type string\n"
+"      type hw\n"
+"      card $CARD\n"
+"}\n"
+"pcm.hw {\n"
+"      @args [ CARD DEV SUBDEV ]\n"
+"      @args.CARD.type string\n"
+"      @args.DEV.type integer\n"
+"      @args.SUBDEV.type integer\n"
+"      type hw\n"
+"      card $CARD\n"
+"      device $DEV\n"
+"      subdevice $SUBDEV\n"
+"}\n"
+;
+
+#ifdef SND_LIB_VER
+#if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
+#define LIB_HAS_LOAD_STRING
+#endif
+#endif
+
+#ifndef LIB_HAS_LOAD_STRING
+static int snd_config_load_string(snd_config_t **config, const char *s,
+                                 size_t size)
+{
+       snd_input_t *input;
+       snd_config_t *dst;
+       int err;
+
+       assert(config && s);
+       if (size == 0)
+               size = strlen(s);
+       err = snd_input_buffer_open(&input, s, size);
+       if (err < 0)
+               return err;
+       err = snd_config_top(&dst);
+       if (err < 0) {
+               snd_input_close(input);
+               return err;
+       }
+       err = snd_config_load(dst, input);
+       snd_input_close(input);
+       if (err < 0) {
+               snd_config_delete(dst);
+               return err;
+       }
+       *config = dst;
+       return 0;
+}
+#endif
+
+snd_config_t *get_alsalib_config(void)
+{
+       snd_config_t *config;
+       int err;
+
+       err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
+       if (err < 0) {
+               ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
+                              snd_strerror(err));
+               ksft_exit_fail();
+       }
+       return config;
+}
+
 static struct card_data *conf_data_by_card(int card, bool msg)
 {
        struct card_data *conf;
index a38b89c280306fe2a02081a3e554d3df75290b42..05f1749ae19d62d699b933c1f5ce191b5a2f5989 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdint.h>
 
 #include "../kselftest.h"
+#include "alsa-local.h"
 
 #define TESTS_PER_CONTROL 7
 
@@ -50,56 +51,11 @@ struct ctl_data {
        struct ctl_data *next;
 };
 
-static const char *alsa_config =
-"ctl.hw {\n"
-"      @args [ CARD ]\n"
-"      @args.CARD.type string\n"
-"      type hw\n"
-"      card $CARD\n"
-"}\n"
-;
-
 int num_cards = 0;
 int num_controls = 0;
 struct card_data *card_list = NULL;
 struct ctl_data *ctl_list = NULL;
 
-#ifdef SND_LIB_VER
-#if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
-#define LIB_HAS_LOAD_STRING
-#endif
-#endif
-
-#ifndef LIB_HAS_LOAD_STRING
-static int snd_config_load_string(snd_config_t **config, const char *s,
-                                 size_t size)
-{
-       snd_input_t *input;
-       snd_config_t *dst;
-       int err;
-
-       assert(config && s);
-       if (size == 0)
-               size = strlen(s);
-       err = snd_input_buffer_open(&input, s, size);
-       if (err < 0)
-               return err;
-       err = snd_config_top(&dst);
-       if (err < 0) {
-               snd_input_close(input);
-               return err;
-       }
-       err = snd_config_load(dst, input);
-       snd_input_close(input);
-       if (err < 0) {
-               snd_config_delete(dst);
-               return err;
-       }
-       *config = dst;
-       return 0;
-}
-#endif
-
 static void find_controls(void)
 {
        char name[32];
@@ -112,12 +68,7 @@ static void find_controls(void)
        if (snd_card_next(&card) < 0 || card < 0)
                return;
 
-       err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
-       if (err < 0) {
-               ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
-                              snd_strerror(err));
-               ksft_exit_fail();
-       }
+       config = get_alsalib_config();
 
        while (card >= 0) {
                sprintf(name, "hw:%d", card);
index 2814d8f74f82d19efa50f19a8faabb6ec38cb2b3..6e7dfc395b987e8137da670240fb50bebab4a533 100644 (file)
@@ -31,26 +31,6 @@ struct pcm_data {
        struct pcm_data *next;
 };
 
-static const char *alsa_config =
-"ctl.hw {\n"
-"      @args [ CARD ]\n"
-"      @args.CARD.type string\n"
-"      type hw\n"
-"      card $CARD\n"
-"}\n"
-"pcm.hw {\n"
-"      @args [ CARD DEV SUBDEV ]\n"
-"      @args.CARD.type string\n"
-"      @args.DEV.type integer\n"
-"      @args.SUBDEV.type integer\n"
-"      type hw\n"
-"      card $CARD\n"
-"      device $DEV\n"
-"      subdevice $SUBDEV\n"
-"}\n"
-
-;
-
 int num_pcms = 0;
 struct pcm_data *pcm_list = NULL;
 
@@ -77,56 +57,6 @@ long long timestamp_diff_ms(timestamp_t *tstamp)
        return (diff.tv_sec * 1000) + ((diff.tv_nsec + 500000L) / 1000000L);
 }
 
-#ifdef SND_LIB_VER
-#if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
-#define LIB_HAS_LOAD_STRING
-#endif
-#endif
-
-#ifndef LIB_HAS_LOAD_STRING
-static int snd_config_load_string(snd_config_t **config, const char *s,
-                                 size_t size)
-{
-       snd_input_t *input;
-       snd_config_t *dst;
-       int err;
-
-       assert(config && s);
-       if (size == 0)
-               size = strlen(s);
-       err = snd_input_buffer_open(&input, s, size);
-       if (err < 0)
-               return err;
-       err = snd_config_top(&dst);
-       if (err < 0) {
-               snd_input_close(input);
-               return err;
-       }
-       err = snd_config_load(dst, input);
-       snd_input_close(input);
-       if (err < 0) {
-               snd_config_delete(dst);
-               return err;
-       }
-       *config = dst;
-       return 0;
-}
-#endif
-
-static snd_config_t *get_alsalib_config(void)
-{
-       snd_config_t *config;
-       int err;
-
-       err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
-       if (err < 0) {
-               ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
-                              snd_strerror(err));
-               ksft_exit_fail();
-       }
-       return config;
-}
-
 static long device_from_id(snd_config_t *node)
 {
        const char *id;