[ccan] [PATCH 5/5] cpuid: only compile source file if x86 cpu

David Gibson david at gibson.dropbear.id.au
Tue Sep 24 13:59:47 EST 2013


On Mon, Sep 23, 2013 at 10:42:01PM +0000, Ahmed Samy wrote:
> Suggested-by: David Gibson <david at gibson.dropbear.id.au>
> Signed-off-by: Ahmed Samy <f.fallen45 at gmail.com>

Uh.. not quite what I had in mind.  What you have here means a full
build will always generate a warning on non-x86 which isn't nice.

What I was suggesting was populating the non-x86 side of the .h with
static inlines using BUILD_ASSERT, so that you'll get a warning if you
actually try to use any of the functions from here on non-x86.

> ---
>  ccan/cpuid/cpuid.c      |  8 ++++++++
>  ccan/cpuid/issupprted.S |  5 +++++
>  ccan/cpuid/test/run.c   | 11 ++++-------
>  3 files changed, 17 insertions(+), 7 deletions(-)
> 
> diff --git a/ccan/cpuid/cpuid.c b/ccan/cpuid/cpuid.c
> index 6ab0e50..6a45116 100644
> --- a/ccan/cpuid/cpuid.c
> +++ b/ccan/cpuid/cpuid.c
> @@ -22,6 +22,10 @@
>   * This file has been written with some help from wikipedia:
>   * 	http://en.wikipedia.org/wiki/CPUID
>   */
> +
> +/* Only compile this file if we're on a x86 machine.  */
> +#if defined(__i386__) || defined(__i386) || defined(__x86_64) \
> +	|| defined(_M_AMD64) || defined(__M_X64)
>  #include <stdint.h>
>  #include <string.h>
>  
> @@ -248,3 +252,7 @@ void cpuid(cpuid_t info, void *buf)
>  	}
>  }
>  
> +#else
> +#warning "Cannot compile this file on a non-x86 machine"
> +#endif
> +
> diff --git a/ccan/cpuid/issupprted.S b/ccan/cpuid/issupprted.S
> index 8fe8596..80ed0fd 100644
> --- a/ccan/cpuid/issupprted.S
> +++ b/ccan/cpuid/issupprted.S
> @@ -2,6 +2,10 @@
>    Test if the CPUID instruction is available.
>    returns 1 if so, 0 otherwise.  */
>  
> +/* Only compile this file if we're on a x86 machine.  */
> +#if defined(__i386__) || defined(__i386) || defined(__x86_64) \
> +	|| defined(_M_AMD64) || defined(__M_X64)
> +
>  .section .text
>  .global cpuid_is_supported
>  .type cpuid_is_supported, @function
> @@ -26,4 +30,5 @@ cpuid_is_supported:
>  	ret
>  
>  	.size cpuid_is_supported, .-cpuid_is_supported
> +#endif
>  
> diff --git a/ccan/cpuid/test/run.c b/ccan/cpuid/test/run.c
> index 1f523e4..21e9aa6 100644
> --- a/ccan/cpuid/test/run.c
> +++ b/ccan/cpuid/test/run.c
> @@ -10,16 +10,13 @@ int main()
>  		return 1;
>  	}
>  
> -	char buf[128];
> -	cpuid(CPU_VENDORID, buf);
> -	printf ("Vendor ID: %s\n", buf);
> +	printf ("Vendor ID: %s\n", cpuid_get_cpu_type_string (cpuid_get_cpu_type ()));
>  
> +	char buf[48];
>  	cpuid(CPU_PROC_BRAND_STRING, buf);
>  	printf ("Processor Brand: %s\n", buf);
>  
> -	int addr;
> -	cpuid(CPU_HIGHEST_EXTENDED_FUNCTION_SUPPORTED, &addr);
> -	printf ("Highest extended function supported: %#010x\n", addr);
> +	printf ("Highest extended function supported: %#010x\n", cpuid_highest_ext_func_supported());
>  
>  	union {
>  		struct {
> @@ -48,7 +45,7 @@ int main()
>  	} l2c;
>  
>  	cpuid(CPU_EXTENDED_L2_CACHE_FEATURES, &l2c.w);
> -	printf ("L2 Cache Size: %u KB\tLine Size: %u bytes\tAssociativity: %02xh\n",
> +	printf ("L2 Cache Size: %ld KB\tLine Size: %ld bytes\tAssociativity: %02xh\n",
>  			l2c.cache_size, l2c.line_size, l2c.assoc);
>  
>  	int invalid;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20130924/1b5b0b98/attachment.sig>


More information about the ccan mailing list